PDA

View Full Version : Can all scripts be saved as .js files


PhilW
03-26-2003, 09:47 AM
I would like to keep javascript code out of my pages so that search engines are more friendly
Can any of the java script just be saved as a .js file and somehow called from the page you want the script on.
As a novice I have tried to look for help on this but I cannot find a guide on how to do it (what kind of scripts will and won't) and what to place on the page (head or body or both?).
Can anyone point me to a novice friendly answer or expain if they have the time please

glenngv
03-26-2003, 09:53 AM
you can save any javascript as a .js file. On where to put whether head or body, it depends. If the contents of the js file are functions, you can put it inside the head. If the content has document.write's (meaning it is called AS the page loads not after the page has loaded), you can put it anywhere inside the body where you want it on.

normally, you would put scripts in a .js when the scripts are being used in differrent pages. If it's only used in a single page, it's advisable to put it inline with the html.

liorean
03-26-2003, 10:05 AM
The recommended method for including external scripts is to put them in the head of the document using <script src="[string URL]" type="text/javascript"></script> syntax. Scripts in the head will be loaded while the document is being parsed, so don't count on their contents being available in the order they are declared. If you're putting the script in the body, use defer="defer" if you want the document rendering to continue while the script loads. (In other words, if you don't use document.write/.writeln in the script).

As for the script itself, just remember that the <!-- //--> were needed in html because you wanted to hide the script source from old browsers - they are invalid in a .js file.

PhilW
03-26-2003, 10:22 AM
Thanks for your help.
I can now see the code to place it in the head, but say this is to rotate sliding images, what do I place in the table cell where I want the sliding images to appear.
Sorry for being thick but I do not understand the defer bit
Regards
Phil

liorean
03-26-2003, 10:29 AM
If you post the current source, maybe we could help you more - it's hard to tell you what to put in the table cell when we don't know what the script looks like.

As for the defer attribute, if you don't have it, the html rendering halts as the browser waits for document.write or document.writeln to be parsed before if continues to render the contents of the html file. If you on the other hand use it, the browser will continue to render the html while executing the script, since it knows the script doesn't change the html in any way. Thus using the defer attribute speeds up the page rendering if you don't use document.write in your script.

PhilW
03-26-2003, 10:48 AM
Thanks
As an example this site www.folletts.com , I have placed a sliding rotating image script.
the instructions said ' just place where you want it to appear'. So in FrontPage2002, I click in the table cell and use an 'InsertHTML' function, paste in the script and it works.
Please could yo uexplain what I place where if converted to a .js file?
Could you also then explain how the procedure would differ where the script had some to be placed in the head and some in the body please, with regard to it being converted to a .js file?

Regards
Phil

glenngv
03-27-2003, 01:55 AM
for question #1
<td>
<script src="[string URL]" type="text/javascript"></script>
</td>

#2
<head>
<script src="[string URL]" type="text/javascript"></script>
</head>
<body>
<script src="[string URL]" type="text/javascript"></script>
</body>

PhilW
03-27-2003, 09:58 AM
Thanks very much for your time Glenngv.
I don't want to sound too sure that I have got it but from your examples, I understand, although it does sound too simple.

Is it as simple as in 2, for every script that says 'paste this bit in the head and this in the body' that you just place the statement you used, altered of course for the file into both places?
Regards
Phil

cheesebagpipe
03-27-2003, 10:17 PM
<td width="20%" rowspan="2" style="text-align: left">
<script language="JavaScript1.2" src="slideshow.js">
</script></td>