0

How could I specifically get every of these query strings in

file:///K:/CKaing_C20_A01_Casino2/game.html?First+Name=Testfirst&Last+Name=Testlast&pnum=123-456-7890&postCode=A1A+1A1&startMoney=5000

For example, I want to get Testfirst, and then assign it to a variable so I can use it later on. Same thing with the others.

This is what I have so far to remove all the +, =

var formData = location.search;

formData = formData.substring(1, formData.length);

while (formData.indexOf("+") != -1) {
    formData = formData.replace("+", " ");
}

formData = unescape(formData);
var formArray = formData.split("&");
for (var i=0; i < formArray.length; ++i) {
    document.writeln(formArray[i] + "<br />");
}

4 Answers 4

1
var splitSearch = JSON.parse("{\""+(location.search.substr(1).replace(/\=/g,"\"\:\"").replace(/\&|(\/\?)/g,"\", \""))+"\"}")

I made that one for a webpage that uses a rare ("/?") separator too.

http://example.com/?a=0&b=bee/?c=third

First one will work for URLs like that

If you want it for a conventional location:

var splitSearch = JSON.parse("{\""+(location.search.substr(1).replace(/\=/g,"\"\:\"").replace(/\&/g,"\", \""))+"\"}")

Once splitSearch is defined you can get "pnum" string like this:

splitSearch.pnum
splitSearch["pnum"]


Another way to get it:

var splitSearch = JSON.parse("{\""+(location.search.substr(1).replace(/(\=)|(\&)|(\/\?)/g, function(k) {
 var rtn=k;
 if (k == "\=") rtn="\"\:\"";
 else if ((k == "\&") /*|| (k == "\/\?")*/) rtn="\",\"";
 return rtn;
})+"\"}"))
Sign up to request clarification or add additional context in comments.

Comments

0

A mix of use of replace with regEx and the split function does the work.

var str = "file:///K:/CKaing_C20_A01_Casino2/game.html?
     First+Name=Testfirst&Last+Name=Testlast
     &pnum=123-456-7890&postCode=A1A+1A1&startMoney=5000";

var argStrIndex = str.indexOf("?");
var argStr = str.substring(argStrIndex+1);
var args = argStr.replace(/\+/g," ").split("&");

for (var i=0;i<args.length;i++){
     alert(args[i]);   
}

demo: http://jsfiddle.net/7meAv/

Comments

0

something like that :

var search = location.search
    .replace(/^\?/,'')
    .replace(/\+/g,' ')
    .split('&')
    .map(function(string){ 
        var split = string.split('=');
        var res={};
        res[split[0]]=split[1];
        return res;
});

should return

[{"First Name":"Testfirst"},{"Last Name":"Testlast"},{"pnum":"123-456-7890"},{"postCode":"A1A 1A1"},{"startMoney":"5000"}]"

You'd need to take care of url encoding though.

Comments

0

A combination of the two answers already given (jsfiddle: http://jsfiddle.net/russianator/GymEq/)

var url = 'file:///K:/CKaing_C20_A01_Casino2/game.html?First+Name=Testfirst&Last+Name=Testlast&pnum=123-456-7890&postCode=A1A+1A1&startMoney=5000';

queryObject = {};
url.substring(url.indexOf('?')+1)
 .replace(/\+/g,' ')
 .split('&')
 .forEach(function(item) {
    splitItem = item.split('=');
    queryObject[splitItem[0]] = splitItem[1];
 });

Returns an object like this:

{
  "First Name": "Testfirst",
  "Last Name": "Testlast",
  ...
}

1 Comment

I like turning things into an object where you can then in turn reference values via a key to get the corresponding value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.