Need to put js into external file

Discussion in 'Scripts' started by techagesite, Mar 11, 2013.

to remove all ads.
  1. techagesite

    techagesite New Member

    Messages:
    4
    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
     
  2. ronaldroe

    ronaldroe Super Moderator Staff Member

    Messages:
    1,223
    <script src="path/to/js/file.js"></script>
     
  3. chrishirst

    chrishirst Well-Known Member Staff Member

    Messages:
    2,639
    And what has this issue to with SEO???
     
  4. ronaldroe

    ronaldroe Super Moderator Staff Member

    Messages:
    1,223
    Hrmmm. Hadn't noticed it was in the wrong forum.
     
  5. techagesite

    techagesite New Member

    Messages:
    4
    Page speed is part of seo

    <script src="path/to/js/file.js"></script>

    im not big on coding, can you describe this snippet?
     
  6. Phreaddee

    Phreaddee Super Moderator Staff Member

    Messages:
    1,978
    well. that's debatable...

    simply put its a script tag. in it is the link to the external file.
    ???
     
  7. chrishirst

    chrishirst Well-Known Member Staff Member

    Messages:
    2,639
    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.

    Convert every javascript call to a function call, pass parameters to the function then display the return value.
     
  8. techagesite

    techagesite New Member

    Messages:
    4
    all i know is that googles page speed tool suggests this and a few other improvements.
     
  9. techagesite

    techagesite New Member

    Messages:
    4
    ive got no idea on that lol maybe i should give it a miss if i dont even understand that jargon
     
  10. chrishirst

    chrishirst Well-Known Member Staff Member

    Messages:
    2,639
    That bit was covered in the second post.
     
  11. chrishirst

    chrishirst Well-Known Member Staff Member

    Messages:
    2,639
    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.
     

Share This Page