Forum Moderators: open

Message Too Old, No Replies

Pass an argument to an external JavaScript file

Is this possible?

         

Fotiman

3:28 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



On my page, I include an external JavaScript file:

<script type="text/javascript" src="myscript.js"></script>

I'd like to know if it's possible to pass an argument to that script file, and if so, how do I read it in the external file? Something like this perhaps:?

<script type="text/javascript" src="myscript.js?id=123"></script>

Then in the JavaScript file, I need some way to get the value of "id".

Thoughts?

DrDoc

3:55 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, normally location.search would work, but in this case it won't, as location.search reads the querystring passed to the page, not individual scripts. You would need to set the variable prior to loading the external script.

<script type="text/javascript">
var id = 123;
</script>
<script type="text/javascript" src="myscript.js"></script>

jshanman

4:04 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



You could also create a server page (PHP,ASP,JSP) and have it output JS. Then you could submit either POST or GET data...

JS:
<script type="text/javascript" src="myscript.php?id=123"></script>

PHP:
echo "alert('".str_replace("'","&#39;",$_GET{'id'})."');";

- JS

DrDoc

4:06 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... provided that the PHP/ASP/Perl/whatever spits out the appropriate Content-Type header first.

Bernard Marx

4:07 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<Pipped at the post!>

Indeed. This <post #2> is probably the most straightforward thing to do (depending on your actual needs, of course).

A more complicated approach is to serve the script file dynamically.
A bare bones ASP example:

--- TEST PAGE ---

<script src="script.asp?name=brian" type="text/javascript"></script>

--- script.asp ---

alert("[blue]<%= Request.QueryString("name") %>[/blue]")

-------------------

I tested this on IIS, with Internet Explorer. Stricter browsers may require you to play with content headers in the ASP file.

Fotiman

4:18 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks everyone. I was hoping for a simple, straightforward approach that didn't require me to use PHP to generate my JavaScript, and which didn't require me to define any inline JavaScript variables first. It looks like that's not possible, though the simplest apprach seems to be to put the value into a variable first, so I'm going to go with that (that way there are no dependencies on PHP or the like, and my JavaScript library can be reused on different platforms).

Thanks.

jshanman

4:30 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



If your creating a tool of some sort, you could include the parameters in the argument of the object you are creating...

var someControl = new MyControl({
setting1: "value1",
setting2: "value2"
});

In this JS file:

function MyControl(params) {
this = params;
... do other stuff...
}

Depending on your tool and application of course...
- JS