Put CodePen JavaScript in <head> Instead of </body>

前端 未结 1 1295
北海茫月
北海茫月 2021-01-22 11:19

In using CodePen, the JavaScript code in the JavaScript pane seems to execute just before the . In my case I have some

1条回答
  •  盖世英雄少女心
    2021-01-22 11:27

    I originally wanted to be able to specify where the JS pane's content is included: In the element or just prior to closing the . By default, CodePen inserts your JS pane content just prior to the close of the and there's nothing you can do about it for now (July, 2015).

    There is something of a hack you can use to get around this though. Let's say your pen is at: http://codepen.io/lanshen/pen/j7GB5q (I just made that up). Your JS pane has its own URL of: http://codepen.io/lanshen/pen/j7GB5q.js

    In the "Stuff for " section in your pen's "Settings", add a tag that refers to your JS pane:

    
    

    This will cause your JS pane's content to be included in the . The obvious problem with this approach is that the JS pane's content will still be included just prior to the (i.e., it will be included twice). To get around this problem, I structured my JS pane content into an if()/else() so that the if() piece will be loaded in and the else piece will be loaded just prior to . Below is the JS pane "template" I used. Again, make sure you reference the JS pane with a

    XYZ

    0 讨论(0)
提交回复
热议问题