How to call javascript function

Discussion in 'JavaScript' started by harish_dixit1, Sep 24, 2006.

  1. #1
    Hello friends,

    I m creating a toolbar for Firefox browser which contains some buttons on it.
    and i have written event handlers for these button click events into a .JS file.
    When user will open a our web site in browser and when i we see source
    code for this web page then we find that it has included with another .js file which contains many functions. for example this Js file looks like:

    <script language="javascript" src="Javascripts/ToolBar.js"></script>



    Now i want to call these functions from my toolbar .JS file. How can i call it.

    If i call directly to that function then it gives error to me that function is not defined .

    PLz help me

    thanks in advance.....

    Harish dixit.
     
    harish_dixit1, Sep 24, 2006 IP
  2. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    That's not the way to include a JS file in another JS file. Insead, try a function like the following:

    
    function include_dom(script_filename) {
        var html_doc = document.getElementsByTagName('head').item(0);
        var js = document.createElement('script');
        js.setAttribute('language', 'javascript');
        js.setAttribute('type', 'text/javascript');
        js.setAttribute('src', script_filename);
        html_doc.appendChild(js);
        return false;
    }
    
    Code (markup):
    And use in the other JS file like this:

    
    include_dom( "Javascripts/ToolBar.js" );
    
    Code (markup):
    More info here.
     
    Evoleto, Sep 25, 2006 IP
  3. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Open the Javascript Console (Tools menu) and you will see all JS errors.

    Regards
    - P
     
    penagate, Sep 27, 2006 IP
  4. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Well, maybe I was wrong when I didn't put it LOUD and CLEAR that the full URL is required ...

    
    include_dom( "http://www.your-site.com/Javascripts/ToolBar.js" );
    
    Code (markup):
    This solution is definitely DOM-compliant BTW
     
    Evoleto, Sep 27, 2006 IP