homepage Welcome to WebmasterWorld Guest from 23.23.22.200
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Where to put javascript
llmm123




msg:3299349
 6:37 pm on Apr 1, 2007 (gmt 0)

Hi all,

I'm totally new to jscript....... I reference my javascript as follows:


<script src="../js/js_bbs.js" type="text/javascript"></script>

As you can see my scripts are in a js folder. Does the fact that I need to move back and forth between folders cause issues with speed?

And then my main concern/question:

Does is matter where I do above references? I.E. Just above </body> tag or just above </head> tag. OR..... is this a stupid question as it depends entirely what the js does. I.E. does it need to load first or last in order for the page to work/display properly.

Thanks,
LL

 

penders




msg:3299540
 12:20 am on Apr 2, 2007 (gmt 0)

Does the fact that I need to move back and forth between folders cause issues with speed?

Speed issue? No. This is the norm. (If there is any issue with resolving file references it is surely negligible?!) You are not really moving back and forth between folders, the 'current' folder is not changing. "../js/js_bbs.js" is a direct file reference, I see no problem with it.

Does is matter where I do above references? I.E. Just above </body> tag or just above </head> tag....

Preferably within the <head>..</head> section. In fact I would always try to reference your JS files here and write your code accordingly - unless there is a problem that prevents you from doing this. (See some later comments in this thread... [webmasterworld.com...] )

But, yes, JS files can be 'referenced' anywhere within your <body>..</body> element too.

[edited by: jatar_k at 3:05 pm (utc) on April 2, 2007]
[edit reason] fixed link [/edit]

ddregallo




msg:3299560
 1:16 am on Apr 2, 2007 (gmt 0)

I would suggest checking out the Javascipt tutorials on w3schools. One of the first lessons is where to put js.

</dan>

Fotiman




msg:3300150
 3:42 pm on Apr 2, 2007 (gmt 0)

It should be noted that there are some who feel the best place for the JS files is not in the head of your document, but instead as the last thing before the closing </body> tag. The reasoning behind this is that downloading of script files will block the content from being delivered, so putting it at the end allows all of the content to be delivered and rendered more quickly, and then your behavior gets loaded at the end. Presumably, this would give you faster rendering of your content.

I have not yet made my own decision as to whether or not this is a valid argument.

Dabrowski




msg:3301491
 10:51 pm on Apr 3, 2007 (gmt 0)

Add the defer parameter to your script tag, as follows:

<script type.... src.... defer></script>

The browser then won't wait for this file to load before it continues with the page, however......

a) make sure nothing on the page is dependant on it, I usually have a few scripts sharing common funtions, so I include a common.js and then defer everything else. If a dependency is missing it'll throw an error as I'm sure you're aware.

b) same goes if you're calling the script in the body somwhere.

c) The window.onload event still won't fire until this file has loaded, or timed out trying.

Fotiman




msg:3302163
 3:00 pm on Apr 4, 2007 (gmt 0)

defer

This attribute was intended to alter the timing of component loading in pages. It is not well supported and should not be used (from what I've read).

[edited by: Fotiman at 3:05 pm (utc) on April 4, 2007]

Dabrowski




msg:3302432
 6:40 pm on Apr 4, 2007 (gmt 0)

Interesting point, I did a bit of Google, and found there is some confusion over the actual meaning of defer, which means some browsers treat it differently.

In that case I would put the script tag at the end of the body, probably safest bet, unless script is on the same server in which case it would do well to fail to load id.

Thanks for the heads up Fotiman.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved