SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Load Javascript Last...

    Hello

    Im 99% sure that a few weeks ago I saw an easy way to make your javascript load after the rest of the page has loaded... however I didnt bookmark the page.

    If anyone knows how to do this then please let me know
    Turn My Text - íspuǝıɹɟ ʞooqǝɔɐɟ ɹnoʎ ǝzɐɯɐ puɐ ǝsnɟuoɔ

    Computer Game Cheats & Codes

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you put your javascript inside window.onload, then it will fire only after the entire page has loaded. E.g.:
    Code:
    window.onload = function() {
      document.getElementsByTagName('p').style.background = 'red';
    }
    That will give all p elements a red background after the page has loaded.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should have been clearer but my knowledge of JS is very poor, however my HTML is
    Code:
    <script language='javascript' src='URL'></script>
    (javascript include I think its called?)

    So the option to use onload isnt there. I *thought* there was something I could put after the language='javascript' part... but I really cant remember.
    Turn My Text - íspuǝıɹɟ ʞooqǝɔɐɟ ɹnoʎ ǝzɐɯɐ puɐ ǝsnɟuoɔ

    Computer Game Cheats & Codes

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script type="text/javascript">
    window.onload = function() {
      document.getElementsByTagName('p').style.background = 'red';
    }
    </script>
    Don't use language="javascript" (note the double quotes, not single quotes), use type="text/javascript" instead. And if you put that in the <head> of your document, it does what I said before. It's the same as doing
    Code:
    <body onload="document.getElementsByTagName('p').style.background = 'red';">
    but putting it in the head in a separate file is much neater. Have a look at this.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    <script src="script.js" type="text/javascript" defer="defer"></script>

    the defer attribute tells browsers that they can delay downloading the actual Javascript file until after the rest of the page has loaded as there is nothing in there that needs to run prior to the onload event being triggered. Not all browsers actually delay the download though as it is a suggestion rather than a requirement.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DEFER!! That was it!! thanks very very much
    Turn My Text - íspuǝıɹɟ ʞooqǝɔɐɟ ɹnoʎ ǝzɐɯɐ puɐ ǝsnɟuoɔ

    Computer Game Cheats & Codes

  7. #7
    SitePoint Enthusiast cubchai's Avatar
    Join Date
    Jul 2006
    Location
    Currently in UK
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nice...I feel that my site loads faster.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •