469,282 Members | 2,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Passing parameters to external JS file?

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

Kinne
Jul 23 '05 #1
5 37276
"kinne" <ki***@advalvas.be> wrote in message
news:41***********************@news.skynet.be...
Is it possible to pass a parameter to an external ".js" file?

Kinne


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.htm</title>
<script type="text/javascript">
var parm = "HelloWorld";
</script>
<script type="text/javascript" src="external.js">
</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?HelloWorld

<html>
<head>
<title>test1.htm</title>
<script type="text/javascript" src="external.js">
</script>
</head>
<body>
</body>
</html>

// external.js
var parm = location.search;
parm = parm.substr(1);
document.write(parm);
Jul 23 '05 #2
"kinne" <ki***@advalvas.be> wrote in message
news:41***********************@news.skynet.be...
Is it possible to pass a parameter to an external ".js" file?

Kinne


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.htm</title>
<script type="text/javascript">
var parm = "HelloWorld";
</script>
<script type="text/javascript" src="external.js">
</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?HelloWorld

<html>
<head>
<title>test1.htm</title>
<script type="text/javascript" src="external.js">
</script>
</head>
<body>
</body>
</html>

// external.js
var parm = location.search;
parm = parm.substr(1);
document.write(parm);
Jul 23 '05 #3
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
[...]
Alternatively, you could pass a parameter via the QueryString portion of the URL:

http://{domain}/{folder}/test2.htm?HelloWorld

<html>
<head>
<title>test1.htm</title>
<script type="text/javascript" src="external.js">
</script>
</head>
<body>
</body>
</html>

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

Jul 23 '05 #4
On Sun, 28 Nov 2004 21:41:39 +0100, kinne <ki***@advalvas.be> wrote:

[snip]
I need to call an external ".js" file and pass a parameter to it.


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(someGlobal); // '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.
Jul 23 '05 #5
[snip]
You cannot "call" a file; it's just plain text. [snip] In summary, you don't need to "pass" anything.

[snip]

OK Michael, I got it now ! Thanks for this clear answer.

Kinne
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Pavils Jurjans | last post: by
2 posts views Thread by Akira | last post: by
reply views Thread by Paul Allan | last post: by
4 posts views Thread by Mike Dinnis | last post: by
1 post views Thread by Jim Phelan | last post: by
2 posts views Thread by Nab | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.