Giter Site home page Giter Site logo

script tags in `head` about week4-3to5 HOT 4 OPEN

jsms90 avatar jsms90 commented on July 29, 2024
script tags in `head`

from week4-3to5.

Comments (4)

dangerdak avatar dangerdak commented on July 29, 2024

@jsms90 The link says "The current state-of-the-art is to put scripts in the tag and use the async or defer attributes. This allows your scripts to be downloaded asap without blocking your browser." at the bottom of the first answer

from week4-3to5.

finnhodgkin avatar finnhodgkin commented on July 29, 2024

I know project time is over but I just noticed this issue.

It's not as clear-cut as "use async because it's the current state-of-the-art". For one thing async isn't supported by IE8.

Other answers in that post include some examples of issues with the async property:

<script src="jquery.js" async></script>
<script>jQuery(something);</script>
/*
  * might throw "jQuery is not defined" error
  * defer will not work either
*/
<script src="jquery.js" async></script>
<script src="jQuery(something).js" async></script>
/*
  * might throw "jQuery is not defined" error (no guarantee which script runs first)
  * defer will work in sane browsers
*/

The above example is particularly relevant. If dom.js was downloaded and executed first it would cause all sorts of problems.

<script src="document.getElementById(header).js" async></script>
<div id="header"></div>
/*
  * might not locate #header (script could fire before parser looks at the next line)
  * defer will work in sane browsers
*/

Putting multiple scripts at the end of the body may not be the best solution but it's definitely the simplest, especially when you have a set of script files that each rely on the last. Async is a great property (especially for below the fold content - both css and js) but shouldn't be used unless you're sure the order of execution doesn't matter.

Further reading (bit old so I'm not sure if it's all still relevant...):
https://www.html5rocks.com/en/tutorials/speed/script-loading/

from week4-3to5.

finnhodgkin avatar finnhodgkin commented on July 29, 2024

(I haven't actually read into this that much so I may be completely wrong 😉)

from week4-3to5.

dangerdak avatar dangerdak commented on July 29, 2024

Ah ok thanks 👍 . We started using it because we saw this issue from a previous week (which refers to the same stackoverflow thread) but probs should have actually read into it rather than assuming we should always use it!

from week4-3to5.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.