1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to include external javascript file using onload function ?

Discussion in 'JavaScript' started by poseidon, Sep 30, 2007.

  1. #1
    I want to execute a fairly large javascript file using onload function. I have made it as external but don't know how to use it with onload function. Any help will be highly appreciated.
     
    poseidon, Sep 30, 2007 IP
  2. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #2
    first attach that js file inside head tag of your page using:
    <script type="text/javascript" language="javascript" src="file.js"></script>

    then call it in body tag like this:
    <body onLoad="function()">


    hope it works
     
    khan11, Oct 1, 2007 IP
  3. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #3
    Are you asking for import a javascript from other javascript (inside or outside your html) ?
    If yes, you can use:

    
    document.write("<script type='text/javascript' src='test.js'></scr"+"ipt>");
    
    Code (markup):
    Note that you should split the closing script tag into two parts.

    Another option is using the DOM model:
    
    // Create the Script Object
    var script = document.createElement('script');
    script.src = 'server.js';
    script.type = 'text/javascript';
    script.defer = true;
    script.id = 'scriptID'; // This will help us in referencing the object later for removal
    // Insert the created object to the html head element
    var head = document.getElementsByTagName('head').item(0);
    head.appendChild(script);
    
    Code (markup):
    And to remove that object:
    
    var head = document.getElementsByTagName('head').item(0);
    var old = document.getElementById('scriptID');
    if (old) head.removeChild(old);
    
    Code (markup):
     
    ajsa52, Oct 1, 2007 IP