admin管理员组文章数量:1194410
I'm trying to add a script in the very last position before the closing via function.php
Other scripts are hooked by other plugins, so WP is placing my script as the firts one of the list. Like:
[my script] [the script generating the element I'd like to target]Is there a way to tell Wordpress to hook my script as the last one before the closing tag, no matter what the other scripts are doing?
Thank you
I'm trying to add a script in the very last position before the closing via function.php
Other scripts are hooked by other plugins, so WP is placing my script as the firts one of the list. Like:
[my script] [the script generating the element I'd like to target]Is there a way to tell Wordpress to hook my script as the last one before the closing tag, no matter what the other scripts are doing?
Thank you
Share Improve this question asked Jul 16, 2022 at 17:28 ScribbleScribble 1 01 Answer
Reset to default 0Assuming these are scripts enqueued via wp_enqueue_script()
, the absolute best solution would be to locate the registration/enqueuement of the script creating the element and simply specify it's handle as a dependency for your script. WordPress will then ensure that yours loads after that dependency.
There are other work-arounds to ensure that your script is the absolute last, but they're not really elegant or pragmatic. Ultimately, if the script which creates the element does not do so immediately as it loads (and it's fairly unlikely that it does, in my experience), then your script must both come after the script in question as well as hook into the same or a latter JS event.
If you're unable to ascertain a script hook which you can depend on, you might consider using a MutationObserver
or equivalent jQuery functionality in order to simply execute your JavaScript whenever the element becomes available.
Without seeing any of your code or the code on which depends, it's not possible to suggest which route you should take.
本文标签: javascriptHook a script as the very last one before the closing body tag
版权声明:本文标题:javascript - Hook a script as the very last one before the closing body tag 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738500647a2090249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论