#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Columbus, Ohio
    Posts
    3
    Rep Power
    0

    Question How do you pass arguments to an external javascript function?


    I would like to pass arguments to an external javascript function but it seem impossible.

    I know how to call the file.

    <SCRIPT LANGUAGE="JavaScript" SRC="TheFile.js"></SCRIPT>

    But how do you pass the arguments to the function.

    Thanks for your help.
  2. #2
  3. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    No such thing as an 'external javascript function'. A function is either loaded in the JS memory space - and available for invocation - or it isn't. Externally-stored JS files are simply kept separate from the documents they're used in, until the document is loaded in the browser (or, occasionally, loaded on demand after page load). How 'bout a better description of the problem?
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Columbus, Ohio
    Posts
    3
    Rep Power
    0

    Ok, This is a better explanation of my problem.


    I want to run a function called TopMenu() which is stored in EngMenu.js file. I want to call this function from an html file and I want to pass two arguments to the function, "vType" and "vRoot".

    The function stored in EngMenu.js file:
    function TopMenu(vType,vRoot) {
    ...........
    return vType;
    }
    document.write(TopMenu());

    Now, how can I call this function from the html file and pass the two arguments.

    When I put:

    <script language="JavaScript">
    document.write('<script "EngMenu.js"><\/script>')
    </script>

    OR

    <SCRIPT language="JavaScript" src="EngMenu.js"></SCRIPT>

    It runs the function automatically and it doesn't let me send any arguments.

    Now, what is the correct way to call this function from the html file and send the arguments.

    Thanks for your help.
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    All right there's a lot of things that are probably causing you problems here. First of all, you say it's running the function automatically without sending any arguments. If you look at the code you posted, after the function definition, it executes the line
    Code:
    document.write(TopMenu());
    which does exactly that: runs the function without any arguments. If you don't want the function to execute automatically, either change the line to read document.write(vType) and put it in the function instead of return vType; or else just remove it and call the function when you need it with
    Code:
    document.write(TopMenu(...));
    using whatever arguments you want to give the function.

    Also your code you specified...
    Code:
    <script language="JavaScript"> 
    document.write('<script "EngMenu.js"><\/script>') 
    </script>
    should have src= in front of "EngMenu.js" and should be like this:
    Code:
    <script type="text/javascript">
    document.write("<script src='EngMenu.js'><\/script>");
    </script>
    The language attribute of the script tag has been replaced by the type attribute; it is still supported but if you are going to write your code according to the newest specs you should use the type attribute instead. Eventually you may find that the language attribute is dropped altogether and it would be better if all of your pages already used the type attribute.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Columbus, Ohio
    Posts
    3
    Rep Power
    0

    Thumbs up Thank you so much. I Solve the problem.


    First of all I changed the function and eliminate the line :

    document.write(TopMenu());

    Now The function stored in EngMenu.js file:
    function TopMenu(vType,vRoot) {
    ...........
    return vType;
    }

    Second in the Html file at the HEADER :

    <SCRIPT language="JavaScript" src="EngMenu.js"></SCRIPT>

    Then in the BODY :

    <SCRIPT language="JavaScript">
    document.write(TopMenu('Value','Value'));
    </SCRIPT>

    It works good for me !!
    Thanks a lot for your help.
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2003
    Posts
    804
    Rep Power
    101
    You're welcome.

IMN logo majestic logo threadwatch logo seochat tools logo