javascript - why does require.js go inside head tag - Stack Overflow

According to many sources such as Yahoo Developer Network, javascript goes at the bottom of the page so

According to many sources such as Yahoo Developer Network, javascript goes at the bottom of the page so web page content will display before javascript is loaded. Does putting call to require.js in the head tag will make a browser wait for the script to finish loading before displaying a page?

<head>
<title>My Sample Project</title>
<script data-main="scripts/main" src="scripts/require.js"></script>
</head> 

According to many sources such as Yahoo Developer Network, javascript goes at the bottom of the page so web page content will display before javascript is loaded. Does putting call to require.js in the head tag will make a browser wait for the script to finish loading before displaying a page?

<head>
<title>My Sample Project</title>
<script data-main="scripts/main" src="scripts/require.js"></script>
</head> 
Share Improve this question edited Feb 12, 2013 at 22:23 Kijewski 26.1k14 gold badges107 silver badges147 bronze badges asked Feb 12, 2013 at 17:57 DmitryDmitry 4,37311 gold badges49 silver badges58 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

The answer is a bit plex; It can go in either the <head> or <body> of your page, depending on what you're requiring with require.js. Certain operations logically need to happen before the content is loaded, but most things are happy to wait.

An example of something which needs to happen before the page loads is a css pre-processor like LESS, this obviously needs to run in the <head> (in reality this should probably be pre-piled and served as static css, but it serves as a good example) - another example would be a JavaScript template system or some kind of client side MVC system like ember.js

For basic presentational JavaScript it's usually safe to include it at the bottom of the page.

The answer is yes. That's the same thing for all the scripts in the head tag. A script block parallel downloads and the browser continues rendering the page once it's finish being executed.

It depends on the content of the javascript file, when it is going to be executed.

e.g.

<script>
  function load() {
    alert("load event!");
  }
  window.onload = load;
</script>

the message box will display after the page is loaded.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742412429a4439077.html

相关推荐

  • javascript - why does require.js go inside head tag - Stack Overflow

    According to many sources such as Yahoo Developer Network, javascript goes at the bottom of the page so

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信