Braxton Posted June 9, 2012 Share Posted June 9, 2012 Sometimes I read that folks include the script tag(with javascript files) between head tags and other times I read about folks placing the script tags directly before the closing body tag. Which is the more sufficient way to do this? Does it really even matter? Quote Link to comment https://forums.phpfreaks.com/topic/263925-where-to-place-script-tags/ Share on other sites More sharing options...
Mahngiel Posted June 10, 2012 Share Posted June 10, 2012 Does it really even matter? 99% of the time, no. Quote Link to comment https://forums.phpfreaks.com/topic/263925-where-to-place-script-tags/#findComment-1352549 Share on other sites More sharing options...
Stefany93 Posted June 11, 2012 Share Posted June 11, 2012 The nicest way to include JS to your HTML is with a separate .js file linked with the src attribute of the script tag in the head section of the site. Otherwise if you mix both JS and HTML your code will get messy and the pageload time will increase significatnly. Quote Link to comment https://forums.phpfreaks.com/topic/263925-where-to-place-script-tags/#findComment-1352862 Share on other sites More sharing options...
kicken Posted June 11, 2012 Share Posted June 11, 2012 If the script does not need to modify the document during the load process (which most don't) then it is generally recommended to put the script before the </body> tag. Doing so allows the browser to get the HTML document rendered to the screen before it spends time loading the scripts for the page. Generally though it doesn't really matter where you put them. Quote Link to comment https://forums.phpfreaks.com/topic/263925-where-to-place-script-tags/#findComment-1352974 Share on other sites More sharing options...
BuildMyWeb Posted June 30, 2012 Share Posted June 30, 2012 If the script does not need to modify the document during the load process (which most don't) i just dont know if id agree with this wholeheartedly. Quote Link to comment https://forums.phpfreaks.com/topic/263925-where-to-place-script-tags/#findComment-1358178 Share on other sites More sharing options...
kicken Posted June 30, 2012 Share Posted June 30, 2012 Why not? The statement is true, in my experience and observations anyway. Quote Link to comment https://forums.phpfreaks.com/topic/263925-where-to-place-script-tags/#findComment-1358201 Share on other sites More sharing options...
haku Posted July 1, 2012 Share Posted July 1, 2012 Kicken is correct. Each script requires a separate HTTP request. Browser limitations mean that only two concurrent requests can be made to each domain. If you have a bunch of images and scripts on your page, and you put your scripts in the head, other elements have to wait for scripts to download before they can be downloaded, slowing down the pageload for the main content. On top of this, scripts are immediately executed upon download (which is why you generally need onload functions in JS), which takes resources, and slows the page load down even more. By putting scripts that don't contain functionality required during page load at the end of the page, it ensures that all other content is loaded first, for a better user experience. This isn't even a new debate - this has been accepted as a best practice for years. Look at the section "put scripts at the bottom" on this page: http://developer.yahoo.com/performance/rules.html/ Quote Link to comment https://forums.phpfreaks.com/topic/263925-where-to-place-script-tags/#findComment-1358249 Share on other sites More sharing options...
Braxton Posted July 28, 2012 Author Share Posted July 28, 2012 Thanks for all the feedback and the link regarding best practices. Greatly appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/263925-where-to-place-script-tags/#findComment-1365078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.