<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...
LVL 3
root_startAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
danny_ebbersCommented:
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
0
 
BatalfCommented:
Why do you need to do this??

If you declare the variables before

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

then they are available inside functions js.

example:

<script type="text/javascript">
var arg1='This';
</script>
<script type='text/javascript' src='functions.js'></script>

Now, arg1 will be available in all functions in functions.inc.
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
jwyrickCommented:
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>
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Michel PlungjanIT ExpertCommented:
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
0
 
SnowFlakeCommented:
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.


0
 
Michel PlungjanIT ExpertCommented:
"better" depends on situation.. Different yes...
0
 
SnowFlakeCommented:
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".
0
 
root_startAuthor Commented:
Thanks a lot Batalf...
It was what I was looking for...
0
 
root_startAuthor Commented:
You helped me a lot.

...root...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.