Welcome to WebmasterWorld Guest from 184.73.13.66

Forum Moderators: open

Message Too Old, No Replies

JavaScript and URL Info

via HTTP GET

   
6:25 pm on Feb 8, 2002 (gmt 0)

10+ Year Member



Hi all,
I want to know if anyone knows of a way that Client-Side JavaScript can be used to get the information off of a URL string. (Example -- [somesite.com...]

I can't find anything on it. Just wanna see if anyone here can please come to the rescue!

Thanks :)

6:32 pm on Feb 8, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can access the querystring section using (the ? onwards)

window.location.search

for the main part of the URL (up to the ?) just use

window.location

6:43 pm on Feb 8, 2002 (gmt 0)

10+ Year Member



Cool,
Now to stripping the unwanted info. How do I go about doing that?

?string=20

The bolded part is what I want. I'm sure there is a function in JS or would I have to go about building one?

thanks

12:02 am on Feb 9, 2002 (gmt 0)

10+ Year Member



The following function returns an associative array of the values. Using your URL example:

var urlArgs = cbeGetURLArguments();

var s = parseInt(urlArgs['string']);


function cbeGetURLArguments() {
var idx = location.href.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = location.href.substring(idx+1, location.href.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs[i].split('=');
params[i] = nameVal[1];
params[nameVal[0]] = nameVal[1];
}
}
return params;
}
7:27 am on Feb 10, 2002 (gmt 0)

10+ Year Member



Hi,

I ended up doing this and it worked great. I have the comment code on here in case someone else desides to use it.

// Grab the querystring if there is one.
var querystring = window.location.search;

// Strip the unwanted data and grab the number.
var querystringValue = querystring.substring(querystring.indexOf("=") +1, querystring.length);

If you are grabbing a number from the querystring, add a -0 at the end or else JS is going to think it's just another normal character that has no numeric value.

// Here we have to add "-0" at the
// end of the variable so that it
// treats it as a numeric value.

var number = querystringValue-0;

Thank you all :)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month