admin管理员组文章数量:1317906
First I enqueue a script using:
wp_enqueue_script( "script", plugins_url( "/test/js/script.js", PATH ), array("jquery"), VERSION, true );
Then I'm inserting an inline script after "script".
wp_add_inline_script( "script", "console.log('hello world');" );
Now I need to add defer or async attribute to my inline script, is there a way to do this to a script embedded by wp_add_inline_script() ?
First I enqueue a script using:
wp_enqueue_script( "script", plugins_url( "/test/js/script.js", PATH ), array("jquery"), VERSION, true );
Then I'm inserting an inline script after "script".
wp_add_inline_script( "script", "console.log('hello world');" );
Now I need to add defer or async attribute to my inline script, is there a way to do this to a script embedded by wp_add_inline_script() ?
Share Improve this question asked Mar 14, 2018 at 15:44 NurgielNurgiel 1805 silver badges12 bronze badges2 Answers
Reset to default 5wp_enqueue_script( "script", plugins_url( "/test/js/script.js", PATH ), array("jquery"), VERSION, true );
wp_script_add_data( 'script', 'async/defer' , true );
see more
I know this isn't what you're looking for, but defer
doesn't work unless there's a src
attribute. I.e. it doesn't work on inline scripts.
From the MDN docs:
defer
This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed, but before firing DOMContentLoaded. Scripts with the defer attribute will prevent the DOMContentLoaded event from firing until the script has loaded and finished evaluating. Warning: This attribute must not be used if the src attribute is absent (i.e. for inline scripts), in this case it would have no effect. The defer attribute has no effect on module scripts — they defer by default. Scripts with the defer attribute will execute in the order in which they appear in the document. This attribute allows the elimination of parser-blocking JavaScript where the browser would have to load and evaluate scripts before continuing to parse. async has a similar effect in this case.
本文标签: javascriptHow to add defer or async attribute to wpaddinlinescriptStack Overflow
版权声明:本文标题:javascript - How to add defer or async attribute to wp_add_inline_script? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742022510a2414938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论