?
Solved

<script src='functions.js test'></script> - Arguments to a .js file

Posted on 2005-04-09
9
Medium Priority
?
398 Views
Last Modified: 2010-08-05
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...
0
Comment
Question by:root_start
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 4

Expert Comment

by:danny_ebbers
ID: 13744425
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
 
LVL 32

Accepted Solution

by:
Batalf earned 150 total points
ID: 13744426
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
 
LVL 2

Expert Comment

by:jwyrick
ID: 13744670
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13745143
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
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13746987
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13747013
"better" depends on situation.. Different yes...
0
 
LVL 15

Expert Comment

by:SnowFlake
ID: 13747053
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
 
LVL 3

Author Comment

by:root_start
ID: 13747178
Thanks a lot Batalf...
It was what I was looking for...
0
 
LVL 3

Author Comment

by:root_start
ID: 13747183
You helped me a lot.

...root...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question