How to call javascript on page load?

Discussion in 'Programming' started by dzinga1, Mar 3, 2011.

  1. #1
    Instead of adding dozens of lines of code directly to a web page, I created a .js file and uploaded it to my server. How can I call that file so I can execute the code in .js? Thanks in advance
     
    dzinga1, Mar 3, 2011 IP
  2. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #2
    1. <body onload="javascript: YOUR JS CODE GOES HERE">

    2. include jquery (that is what most of sites using today), and importantly can add as many handlers for load event you want. Download jquery js firt
    a) <script src="jquery-VERSION.js"></script>
    b) $(function() { YOUR JS CODE GOES HERE });

    I hope it helps.
     
    Vooler, Mar 4, 2011 IP
  3. ACME Squares

    ACME Squares Peon

    Messages:
    98
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Does it absolutely have to wait for the onload event?
    If it doesn't, then it's as simple as creating an external JavaScript file, and linking it at the bottom of your code.
     
    ACME Squares, Mar 4, 2011 IP