Passing parameters to external JS file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kinne

    #1

    Passing parameters to external JS file?

    Is it possible to pass a parameter to an external ".js" file?

    Kinne


  • McKirahan

    #2
    Re: Passing parameters to external JS file?

    "kinne" <kinne@advalvas .be> wrote in message
    news:41a8e159$0 $25071$ba620e4c @news.skynet.be ...[color=blue]
    > Is it possible to pass a parameter to an external ".js" file?
    >
    > Kinne[/color]

    Not sure exactly what you mean or want.

    However, you can define a variable outside of the external ".js" file and
    use it within it.

    <html>
    <head>
    <title>test2.ht m</title>
    <script type="text/javascript">
    var parm = "HelloWorld ";
    </script>
    <script type="text/javascript" src="external.j s">
    </script>
    </head>
    <body>
    </body>
    </html>

    // external.js
    document.write( parm);

    Alternatively, you could pass a parameter via the QueryString portion of the
    URL:

    http://{domain}/{folder}/test2.htm?Hello World

    <html>
    <head>
    <title>test1.ht m</title>
    <script type="text/javascript" src="external.j s">
    </script>
    </head>
    <body>
    </body>
    </html>

    // external.js
    var parm = location.search ;
    parm = parm.substr(1);
    document.write( parm);


    Comment

    • McKirahan

      #3
      Re: Passing parameters to external JS file?

      "kinne" <kinne@advalvas .be> wrote in message
      news:41a8e159$0 $25071$ba620e4c @news.skynet.be ...[color=blue]
      > Is it possible to pass a parameter to an external ".js" file?
      >
      > Kinne
      >[/color]

      Not sure exactly what you mean or want.

      However, you can define a variable outside of the external ".js" file and
      use it within it.

      <html>
      <head>
      <title>test2.ht m</title>
      <script type="text/javascript">
      var parm = "HelloWorld ";
      </script>
      <script type="text/javascript" src="external.j s">
      </script>
      </head>
      <body>
      </body>
      </html>

      // external.js
      document.write( parm);

      Alternatively, you could pass a parameter via the QueryString portion of the
      URL:

      http://{domain}/{folder}/test2.htm?Hello World

      <html>
      <head>
      <title>test1.ht m</title>
      <script type="text/javascript" src="external.j s">
      </script>
      </head>
      <body>
      </body>
      </html>

      // external.js
      var parm = location.search ;
      parm = parm.substr(1);
      document.write( parm);


      Comment

      • kinne

        #4
        Re: Passing parameters to external JS file?

        Thanks for replying!
        My question was indeed very short, but the second part of you answer was the
        kind of thing that I expected.
        I need to call an external ".js" file and pass a parameter to it. This
        parameter would be used in the file as a variable to perform some tests.
        What I don't know is i) how to pass the variable to the code in the external
        ".js" file and ii) how to collect the passed variable to work with it within
        the file.
        The QueryString looks like to way to do this. Now that I have a starting
        point, I'll try to find my way and more info about this. Thanks for giving
        me the first clue.

        Kinne


        [...][color=blue]
        > Alternatively, you could pass a parameter via the QueryString portion of[/color]
        the[color=blue]
        > URL:
        >
        > http://{domain}/{folder}/test2.htm?Hello World
        >
        > <html>
        > <head>
        > <title>test1.ht m</title>
        > <script type="text/javascript" src="external.j s">
        > </script>
        > </head>
        > <body>
        > </body>
        > </html>
        >
        > // external.js
        > var parm = location.search ;
        > parm = parm.substr(1);
        > document.write( parm);
        >
        >[/color]


        Comment

        • Michael Winter

          #5
          Re: Passing parameters to external JS file?

          On Sun, 28 Nov 2004 21:41:39 +0100, kinne <kinne@advalvas .be> wrote:

          [snip]
          [color=blue]
          > I need to call an external ".js" file and pass a parameter to it.[/color]

          You cannot "call" a file; it's just plain text.

          A SCRIPT element with a src attribute is effectively the same as a SCRIPT
          element with the file contents included directly in it. Any script added
          to a document shares the same global namespace, so the code in the second
          SCRIPT element below can access the variables defined and initialised in
          the first, irrespective of whether the code is stored in an external file
          or included directly in the document.

          <script type="text/javascript">
          var someGlobal = 'A variable';
          </script>

          <script type="text/javascript">
          alert(someGloba l); // 'A variable'
          </script>

          In summary, you don't need to "pass" anything.

          [snip]

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • kinne

            #6
            Re: Passing parameters to external JS file?

            [snip][color=blue]
            > You cannot "call" a file; it's just plain text.[/color]
            [snip][color=blue]
            > In summary, you don't need to "pass" anything.[/color]
            [snip]
            [color=blue][color=green]
            >>[/color][/color]
            OK Michael, I got it now ! Thanks for this clear answer.

            Kinne


            Comment

            Working...