Passing parameters to external JS file?

M

McKirahan

kinne said:
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);
 
M

McKirahan

kinne said:
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);
 
K

kinne

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


[...]
 
M

Michael Winter

[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
 
K

kinne

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top