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.
  2. Better Analytics for WordPress Get It Free

How do I include a Javascript file in a Javascript file?

Discussion in 'JavaScript' started by spacebass5000, Sep 22, 2006.

  1. #1
    I am looking to nest an include for one javascript file in another, how do I do this?
     
    spacebass5000, Sep 22, 2006 IP
  2. discoverclips

    discoverclips Peon

    Messages:
    491
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    file1:
    <script>
    
    function include(destination) {
    var e=window.document.createElement('script');
    e.setAttribute('src',destination);
    window.document.body.appendChild(e);
    }
    </script><body onLoad="include('some-other-file.html');">
    Code (markup):
    some-other-file.html:
    alert('hi');

    you should see a box :)
     
    discoverclips, Sep 22, 2006 IP
  3. spacebass5000

    spacebass5000 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i'm not sure if that is what I am looking for.

    i want to include a javascript file inside another one.
     
    spacebass5000, Sep 22, 2006 IP
  4. discoverclips

    discoverclips Peon

    Messages:
    491
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    include('some-other-file.html');
     
    discoverclips, Sep 23, 2006 IP
  5. jawednazarali

    jawednazarali Guest

    Messages:
    254
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    include('abc.js');
     
    jawednazarali, Sep 23, 2006 IP
  6. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Um, there's no include() function in Javascript.
    discoverclips is correct.

    Also, remember to always use the type attribute on script elements ("text/javascript").

    Regards
    - P
     
    penagate, Sep 27, 2006 IP
  7. jawednazarali

    jawednazarali Guest

    Messages:
    254
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It was my mistake, I didn't realize that include isn't the Javascript function. Actually what we are doing is that we are using a customized function described below:

    
    function include(filename)
    {
    	var head = document.getElementsByTagName('head')[0];
    	
    	script = document.createElement('script');
    	script.src = filename;
    	script.type = 'text/javascript';
    	
    	head.appendChild(script)
    }
    
    Code (markup):
    This function is defined in a file jsHandler.js and we include this file in all our pages at the top. once this file is included you can use include("abc.js"); any where you want.

    Thanks for notifying and I apologize for not providing complete information.
    Let me know if you guys have any problem in using it.
     
    jawednazarali, Sep 27, 2006 IP
  8. M57

    M57 Peon

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    This might interest you (copy and paste URL to your browser):
    mashhoor.ws/2006/07/19/javascript-includes/

    It's not bullet-proof, sometimes it doesn't work for some reason, but it's worth a try.
     
    M57, Sep 30, 2006 IP