Jump to content

Recommended Posts

<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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/246788-js-in-the-body-and-not-the-head/
Share on other sites

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.

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.

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.

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.

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])

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.