By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,369 Members | 1,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,369 IT Pros & Developers. It's quick & easy.

Query String from src

P: n/a
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[i].indexOf('='); "name=value".
if (pos == -1) continue;.
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
args[argname] = unescape(value);
}
alert (args.id);
}
....

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

Can this be done?

Thanks,

dstefani
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Sat, 15 May 2004 12:06:50 GMT, dstefani wrote:
I was wondering if you can get the info from the query string in a
server-side javascript tag?
Yes. (*server-side*?? No.
But I don'y think you mean that)
Here's what I'm trying to do


Here is a (bad) example with some good
links to threads with tips right here..
<http://www.physci.org/test/003url/index.html?url=http://mybiz.com/&when=now>

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Jul 23 '05 #2

P: n/a
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[i].indexOf('=');
if (pos == -1) continue;
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
args[argname] = unescape(value);property.
}
alert (args.id);
}
Jul 23 '05 #3

P: n/a
On Sat, 15 May 2004 14:52:58 GMT, dstefani wrote:
I think I need to clarify...
Spot on there. ;-)
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>
Firstly, it is missing a '>' character..
<script language="JavaScript" src="script.js?id=test"></script>
Can the src script recognize the query string?


OK, now...

What is it that you expect the script.js
file to *do* with the id=test?

If it requires the information, it can get
it from the URL (that your normal link
points to) as shown in the example I pointed to.

** But no, I do not believe there is a way
to hand information to a .js file as you
describe. **

Which leads us to an important question.
What are you actually trying to achieve?

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
Jul 23 '05 #4

P: n/a
Andrew Thompson wrote:
On Sat, 15 May 2004 14:52:58 GMT, dstefani wrote: 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
Jul 23 '05 #5

P: n/a
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.

Jul 23 '05 #6

P: n/a
Lee wrote:
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.


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

- dstefani
Jul 23 '05 #7

P: n/a
Ivo
"dstefani" wrote
Andrew Thompson wrote:
On Sat, 15 May 2004 14:52:58 GMT, dstefani wrote:

Firstly, it is missing a '>' character..
<script language="JavaScript" src="script.js?id=test"></script>


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
Jul 23 '05 #8

P: n/a
Lee
dstefani said:

Lee wrote:
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.


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

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.