homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

JavaScript and URL Info

10+ Year Member

Msg#: 45 posted 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 :)



WebmasterWorld Senior Member 10+ Year Member

Msg#: 45 posted 6:32 pm on Feb 8, 2002 (gmt 0)

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


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



10+ Year Member

Msg#: 45 posted 6:43 pm on Feb 8, 2002 (gmt 0)

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


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?



10+ Year Member

Msg#: 45 posted 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;


10+ Year Member

Msg#: 45 posted 7:27 am on Feb 10, 2002 (gmt 0)


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