Link to home
Start Free TrialLog in
Avatar of root_start
root_startFlag for Brazil

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...
Avatar of danny_ebbers
danny_ebbers

Hi root_start,

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
ASKER CERTIFIED SOLUTION
Avatar of Batalf
Batalf
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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>
Avatar of Michel Plungjan
Batalf is correct. Not only that but you can override values

<script type='text/javascript' src='functions.js'></script>

<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.


"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".
Avatar of root_start

ASKER

Thanks a lot Batalf...
It was what I was looking for...
You helped me a lot.

...root...