Parsing Requesting URL in javascript?

E

Evertjan.

wrote on 20 aug 2007 in comp.lang.javascript:
I have the following request URL in an ASPx page:

http://crm/custom/fileviewer/default.aspx?NAME

I want to be able to parse the requesting URL and pass NAME into a
variable.

Can anyone give me the code to do this?

Not "the" code as there are a miriad of ways:

I give you the clientside string manipulation.

<script type='text/javascript'>

var s = 'http://crm/custom/fileviewer/default.aspx?NAME';

var r = s.split('?')[1]
alert(r)

// or:

r = s.replace(/[^\?]*\?/,'')
alert(r)

</script>
 
A

aonic

I wrote this a few days ago:

It parses query strings, example at bottom

parseQuery = function() {
window.query = {};
var qstr = location.search.substr(1).split('&');
var qstr_len = qstr.length;
for(var i=0; i < qstr_len; i++) {
var q = (qstr ? qstr.split('=') : []);
if(q.length) {
firstBracket = q[0].indexOf('[');
if(firstBracket != -1 && -1 != (secondBracket = q[0].indexOf(']',
firstBracket))) {
var queryString = q[0].substr(0, firstBracket);
window.query[queryString] = window.query[queryString] || [];
if(firstBracket+1 == secondBracket) {
window.query[queryString].push(q[1]);
} else {
queryIndex = q[0].substr(firstBracket+1, (secondBracket-
(firstBracket+1)));
window.query[queryString][queryIndex] = q[1];
}
} else {
window.query[q[0]] = q[1];
}
}
}
window['$_GET'] = window.query;
}

/**
Example:

URL: http://example.com/?aff=raja&c[]=1&x[cool]=2
parseQuery()
window.query['c'][0]
"1"
"raja"
"2"
**/



I have the following request URL in an ASPx page:

http://crm/custom/fileviewer/default.aspx?NAME

I want to be able to parse the requesting URL and pass NAME into a
variable.

Can anyone give me the code to do this?

Cheers
Tim
 
A

aonic

Just to clarify, the function above reads the current URL of the page
and parses the query strings into window.query

I wrote this a few days ago:

It parses query strings, example at bottom

parseQuery = function() {
window.query = {};
var qstr = location.search.substr(1).split('&');
var qstr_len = qstr.length;
for(var i=0; i < qstr_len; i++) {
var q = (qstr ? qstr.split('=') : []);
if(q.length) {
firstBracket = q[0].indexOf('[');
if(firstBracket != -1 && -1 != (secondBracket = q[0].indexOf(']',
firstBracket))) {
var queryString = q[0].substr(0, firstBracket);
window.query[queryString] = window.query[queryString] || [];
if(firstBracket+1 == secondBracket) {
window.query[queryString].push(q[1]);
} else {
queryIndex = q[0].substr(firstBracket+1, (secondBracket-
(firstBracket+1)));
window.query[queryString][queryIndex] = q[1];
}
} else {
window.query[q[0]] = q[1];
}
}
}
window['$_GET'] = window.query;

}

/**
Example:

URL:http://example.com/?aff=raja&c[]=1&x[cool]=2
parseQuery()
window.query['c'][0]
"1"

"raja"
window.query['x']['cool']

"2"
**/


I have the following request URL in an ASPx page:

I want to be able to parse the requesting URL and pass NAME into a
variable.
Can anyone give me the code to do this?
Cheers
Tim
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top