Need to put js into external file

techagesite

New Member
In an effort to reduce my sites loading time i want to put java script code in external files to get them to load up quicker

how do i put the code in a file and still get the code to load up in the same place on the page?

i know very little about coding and need to do this in html
 

chrishirst

Well-Known Member
Staff member
Page speed is part of seo
I agree with Phreaddee on that one, improving the "Visitor experience" maybe; and using 'external' .js files will not make any difference to document loading times on the first visit, it MAY however reduce the loading time, which, by the way is not the same as the rendering time, of subsequent visits and of course the render time will not be significantly improved by caching of .js files.
To be honest, if the javascript makes a significant difference to the document loading times, you should have a rethink over it being necessary or not to the purpose of the document.

how do i put the code in a file and still get the code to load up in the same place on the page?
Convert every javascript call to a function call, pass parameters to the function then display the return value.
 

chrishirst

Well-Known Member
Staff member
Just create a directory/folder named "js" in your server backend file management system in the root directory of your website. Some add the following script tag in the <head> section of an HTML page, but if the javascript does not need to load before the rest of the document, I would put the following script tag at the very end of the HTML page - just before the closing </body> tag.

<script type="text/javascript" src="js/name-of-javascript-file.js"></script>

Hope this helps.

Fake sig link REMOVED

That bit was covered in the second post.
 

chrishirst

Well-Known Member
Staff member
Oh and

HTML:
src="js/name-of-javascript-file.js">

Does NOT reference a file in a root directory, it refernces a file in a SUBDIRECTORY of the current document location, so the JS call would FAIL with a 404 response if the HTML document was in a subdirectory.

eg:
site.tld/page.ext would work

site.tld/subdir/file.ext and the .js would fail to load.
 
Top