root_start
asked on
<script src='functions.js test'></script> - Arguments to a .js file
Hi Experts,
I have a javascript file called: "functions.js" and I would like to pass some arguments to this .js file and get this arguments from inside of the file.
Is it clear what I want to do?
I would like to do the following, check bellow:
<script type='text/javascript' src='functions.js arg1 arg2 arg3'></script>
And inside the "functions.js" I would have something like the following:
var argv = this.arguments;
Is it possible to do something like this?
Thanks for your help.
...root...
I have a javascript file called: "functions.js" and I would like to pass some arguments to this .js file and get this arguments from inside of the file.
Is it clear what I want to do?
I would like to do the following, check bellow:
<script type='text/javascript' src='functions.js arg1 arg2 arg3'></script>
And inside the "functions.js" I would have something like the following:
var argv = this.arguments;
Is it possible to do something like this?
Thanks for your help.
...root...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The src of a script is usually a filepath, not a function. The funtion should go in the script; either in the source file or between the script tags.
here is an example:
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function test(arg1, arg2, arg3)
{
//place your code here
}
//-->
</SCRIPT>
here is an example:
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function test(arg1, arg2, arg3)
{
//place your code here
}
//-->
</SCRIPT>
Batalf is correct. Not only that but you can override values
<script type='text/javascript' src='functions.js'></scrip t>
<script type="text/javascript">
arg1='This';
callMyExternalJsFunction()
</script>
if functions.js looks like this:
// js file
arg1= "That"
function callMyExternalJsFunction() {
alert(arg1)
}
// end of js
then you will get "This" alerted
Michel
PS: points to Batalf
<script type='text/javascript' src='functions.js'></scrip
<script type="text/javascript">
arg1='This';
callMyExternalJsFunction()
</script>
if functions.js looks like this:
// js file
arg1= "That"
function callMyExternalJsFunction()
alert(arg1)
}
// end of js
then you will get "This" alerted
Michel
PS: points to Batalf
mplungjan,
because both your assignments to arg1 are
get executed "on the way"
then the last one counts.
IMHO,
A better approach would be
function callMyExternalJsFunction() {
if (arg1==null) {
arg1="That"; // As A default value
}
alert(arg1)
}
// end of js
or better yet just pass it as a parameter to the function itself.
because both your assignments to arg1 are
get executed "on the way"
then the last one counts.
IMHO,
A better approach would be
function callMyExternalJsFunction()
if (arg1==null) {
arg1="That"; // As A default value
}
alert(arg1)
}
// end of js
or better yet just pass it as a parameter to the function itself.
"better" depends on situation.. Different yes...
Iwont split hairs with you,
I think the pattern of using an explicit value or a preset default
is more maintainable then using a "whatever happend last counts".
I think the pattern of using an explicit value or a preset default
is more maintainable then using a "whatever happend last counts".
ASKER
Thanks a lot Batalf...
It was what I was looking for...
It was what I was looking for...
ASKER
You helped me a lot.
...root...
...root...
no it is not,
but you could use hidden form fields to store values in, because you can read those with javascript
or define global variables somewere else inline your page
Best regards,
Danny Ebbers