freelance84 Posted September 9, 2011 Share Posted September 9, 2011 <script type="text/javascript" src="amazing.js"></script> Is there any drawback of placing the above line in the <body> instead of the <head>? http://robertnyman.com/2008/04/23/where-to-include-javascript-files-in-a-document/ states that it is actually best practise. http://www.quirksmode.org/js/placejs.html warns that doing so will results in error in the very old browsers I have a situation where the js file is only required when a certain 'app' within the site is used, and the said app may occur on a few different pages in a few different places thus the inclusion within the body is going to be far more efficient within the body than manually adding it each time within the head. Further more i do not want to write the js code directly to the source. Has anyone experienced any issues i should watch out for? Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/ Share on other sites More sharing options...
Adam Posted September 9, 2011 Share Posted September 9, 2011 The main benefit is that code placed within the body is executed as the page is loading, instead of after it's finished loading. I wouldn't worry about really old browsers to be honest, just write the code so that it isn't dependant on the JS (which you should anyway), and be aware that the DOM tree will be incomplete. One possible issue to watch out for would be delaying the page draw. If a script hangs nothing else will happen until it's done, so I'd be cautious with third-party scripts that could otherwise be loaded within the head. Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1267383 Share on other sites More sharing options...
freelance84 Posted September 9, 2011 Author Share Posted September 9, 2011 Cool thanks for the info Adam Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1267472 Share on other sites More sharing options...
freelance84 Posted September 12, 2011 Author Share Posted September 12, 2011 just write the code so that it isn't dependant on the JS (which you should anyway) True however how do you work around a program based within a website which relies on AJAX? (facebook) Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268478 Share on other sites More sharing options...
Adam Posted September 12, 2011 Share Posted September 12, 2011 Facebook displays a warning and provides an alternate site when you have JS disabled: JavaScript is disabled on your browser Please enable JavaScript or upgrade to a JavaScript-capable browser to use Facebook. Alternatively, you can access the mobile version of Facebook. Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268481 Share on other sites More sharing options...
freelance84 Posted September 12, 2011 Author Share Posted September 12, 2011 Ah touche, however was this always the case, I think this a relatively recent thing? Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268527 Share on other sites More sharing options...
Adam Posted September 12, 2011 Share Posted September 12, 2011 Writing web sites to function with and without JS? Not at all.. Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268529 Share on other sites More sharing options...
freelance84 Posted September 12, 2011 Author Share Posted September 12, 2011 No, with regard to specifically Facebook, not the idea of writing sites to work with and without js. Im not entirely sure, but i think i remember about a year ago when i checked, facebook simply displayed a message which said words to the effect 'this site needs js to work'. Do you know any other site which work with ajax that function with js switched off? www.magicseaweed.com - Doesn't work without js, and they dont even give you a little note as to why, and their site is huuuge in the surfing world. Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268544 Share on other sites More sharing options...
Adam Posted September 13, 2011 Share Posted September 13, 2011 I can recall something similar, but about cookies. I can't think of any sites off of the top of my head but I'll let you know if I do. The site you linked to is largely accessible without JS, just certain features. I'm not saying there aren't sites out there that have done well without complete non-JS support, I'm just trying to say the correct way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268647 Share on other sites More sharing options...
freelance84 Posted September 13, 2011 Author Share Posted September 13, 2011 Aye, and when i started at the beginning of the learning curve with regard to web design, i followed that ethos/correct way to the letter. However I think it may well be better worded to something akin to 'The correct way of web design is to assess your market and then decide if it would be beneficial for your site to function with js on and off, or just on' (The other site i mentioned was predominately for surf forecasts, thus the main function of the site without js barely functions. Also have you tried logging into hotmail without js [i appreciate that they have a huge already acquired market and can afford to say what they say... but still]) Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268685 Share on other sites More sharing options...
AyKay47 Posted September 13, 2011 Share Posted September 13, 2011 it really comes down to weighing the pros and cons.. where not having functionality in your website without JS enabled really comes in to play is mobile users.. Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268686 Share on other sites More sharing options...
Adam Posted September 13, 2011 Share Posted September 13, 2011 The correct way most feasible way with given resources [...] It isn't "correct" to do it that way, but I agree there are situations where it's not worth it to some degree. Of course that's only for minor functionality though; navigation and transactional functionality for example should never be impacted! The main reason I made a point of saying it though is that this is a learning community, and we should aim to teach everybody the right way, even if they choose not to follow it later. Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268688 Share on other sites More sharing options...
freelance84 Posted September 13, 2011 Author Share Posted September 13, 2011 it really comes down to weighing the pros and cons.. where not having functionality in your website without JS enabled really comes in to play is mobile users.. navigation and transactional functionality for example should never be impacted! I could not agree more. Although learning html/css/php/mysql without JS for me really set a solid foundation, as I am self taught I think it would be beneficial (in hind sight) to make this fact aware to other people starting at the beginning of the curve. I remember being left with the impression that the site should function without JS... no questions... then add JS afterwards. Quote Link to comment https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/#findComment-1268744 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.