Query String from src

D

dstefani

Hello,

I was wondering if you can get the info from the query string in a
server-side javascript tag?

Here's what I'm trying to do


In the head of page A
....
<script language="JavaScript" src="script.js?id=test</script>
....


And in my script.js file
....
getArgs();

function getArgs() {
var args = new Object();
var query = location.search.substring(1);
var pairs = query.split('&');
for(var i = 0; i < pairs.length; i++) {
var pos = pairs.indexOf('='); "name=value".
if (pos == -1) continue;.
var argname = pairs.substring(0,pos);
var value = pairs.substring(pos+1);
args[argname] = unescape(value);
}
alert (args.id);
}
....

I get the alert, but is displays "undefined".

Can this be done?

Thanks,

dstefani
 
D

dstefani

I think I need to clarify...

I would like to know if this is possible...
Sending a query string from this tag:

<script language="JavaScript" src="script.js?id=test"</script>

Can the src script recognize the query string?
I can do it perfectly with an <a href="page.html?id=test">, but I can't
get it to work as noted in the src="script.js?id=test"


Here is whats in my js script, it works fine for a link.

Thanks,

dstefani



getArgs();

function getArgs() {
var args = new Object();
var query = location.search.substring(1);
var pairs = query.split('&');
for(var i = 0; i < pairs.length; i++) {
var pos = pairs.indexOf('=');
if (pos == -1) continue;
var argname = pairs.substring(0,pos);
var value = pairs.substring(pos+1);
args[argname] = unescape(value);property.
}
alert (args.id);
}
 
L

Lee

dstefani said:
Hello,

I was wondering if you can get the info from the query string in a
server-side javascript tag?

Here's what I'm trying to do


In the head of page A
...
<script language="JavaScript" src="script.js?id=test</script>
...


And in my script.js file
...
getArgs();

function getArgs() {
var args = new Object();
var query = location.search.substring(1);


You seem to misunderstand how that script block is loaded
and executed. There is nothing "server-side" about it.
The code is loaded into the current HTML page and executes
in that context, which means that location.search will
always refer to the URL of the current HTML page.
 
D

dstefani

Firstly, it is missing a '>' character..
<script language="JavaScript" src="script.js?id=test"></script>
Which leads us to an important question.
What are you actually trying to achieve?

Thanks for your reply and time.

I am trying to track visitor movement through a site.

1/ use script.js to set a cookie
2/ grab the querystring in the afore mentioned tag to know which site it
is. I guess I could have a lookup table that checked HTTP headers for
the location and mapped it to an account, but it would be easy if I
could get it from that tag.

-dstefani
 
D

dstefani

Lee said:
You seem to misunderstand how that script block is loaded
and executed. There is nothing "server-side" about it.
The code is loaded into the current HTML page and executes
in that context, which means that location.search will
always refer to the URL of the current HTML page.

I knew my stupidity would come into play soon.
Can I change my function to recognize the query string in the scr=""
parameter?

- dstefani
 
I

Ivo

Secondly, the language attribute should be dropped and a type of
text/javascript be specified, so the resulting tag would be:

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

The location object is about the location of the page containing the script.
I don't think I 'm saying anything new. The source of the script itself is
document.scripts[n].src in IE4+ and
document.getElementsByTagName('script')[n].src in some other browsers where
n is the index of the script. This is a string which can be parsed for
questions marks and equal signs.

The extension to the source file name may be .php or .asp or anything as
long as the file itself contains javascript and the server sends the
appropiate headers.

Sadly, a running script does not know from which file it came if there more
than one .js file in a page.
HTH
Ivo
 
L

Lee

dstefani said:
I knew my stupidity would come into play soon.
Can I change my function to recognize the query string in the scr=""
parameter?

That's just ignorance, not stupidity.
The fact that the code is executed in the context of the current
HTML page probably means that there's an easier way to do what
you're trying to accomplish. For example:

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


The value of id will be available to the code within script.js
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top