Click to See Complete Forum and Search --> : passing variables through src


spacefem
11-09-2003, 09:54 AM
I know you all get tons of questions on variable passing, but I seriously can't get this thing to work!

The deal: on one page, I have this:

<script language="JavaScript" SRC="http://spacefem.com/oftheday/code1.js?ted=5"></SCRIPT>

Then I have a file called code1.js that says this:

var ted = location.search.substr(1).split("&")
document.write(ted);
document.write("<hr>");

Now, the <hr> is working, and if I do a var ted=8 or whatever it writes it, but it can't get the variable to the URL. I tried different variations of that location.search... even tried totally leaving it out... but nothing worked. I think it's because I'm referring to a source file, there isn't really a "location" to look at. so how's it supposed to get the variables?

Thanks for your help!

gil davis
11-09-2003, 02:57 PM
You cannot pass a query string to a javascript file.
there isn't really a "location" to look at.
There is, but it is the window.location. The .js file is really a property of the document if anything. Still, you cannot pass any variables in the script tag.

You could do this, though:
<script>
window.location.search = "?ted=5";
</script>
<script language="JavaScript" SRC="http://spacefem.com/oftheday/code1.js"></SCRIPT>
The "?" may not be necessary in some browsers. I seem to recall that some of them always give you a "?" by default, but I could be wrong.