homepage Welcome to WebmasterWorld Guest from 54.167.173.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
JavaScript and URL Info
via HTTP GET
circuitjump




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

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 :)

 

joshie76




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

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

circuitjump




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

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

MikeFoster




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

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;
}

circuitjump




msg:1472879
 7:27 am on Feb 10, 2002 (gmt 0)

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 :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved