/    Sign up×
Community /Pin to ProfileBookmark

how to get data from url?

i need to get some data from the url so i can transfer some data using ajax. but i don’t know the code.
trying to get this data to a JavaScript variable:

[code=html]http://localhost/mywebsite/messaging.php?sub=hi&recip=Admin[/code]

i need to get the “recip” and “sub” data and ideas?

Thanks
Tom

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JunkMaleNov 17.2011 — use the indexOf method to get the position in the string.
[CODE]
url="http://localhost/mywebsite/messaging.php?sub=hi&recip=Admin";
a = url.indexOf("sub=");
b = url.indexOf("&", a);
c = url.indexOf("recip=", a);
sub = url.slice(a+4,b);
recip = url.slice(c+6);
alert("Sub = "+sub+"rnRecip = "+recip);
[/CODE]
Copy linkTweet thisAlerts:
@aj_nscNov 17.2011 — Or, if you'd prefer a function you can add to your toolbox:

<i>
</i>function qs() {
var qsParm = {};
var query = window.location.search.substring(1);
var parms = query.split('&amp;');
for (var i=0; i&lt;parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos &gt; 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}

return qsParm;
}


This gives you the parameters in the form of a JS object like this:

<i>
</i>var parameters = qs();
alert(parameters.sub);
alert(parameters.recip);


The code was modified slightly from this website:

http://javascript.about.com/library/blqs2.htm

You may prefer the code from one of these other two sites better:

http://www.netlobo.com/url_query_string_javascript.html

http://www.bennadel.com/blog/695-Ask-Ben-Getting-Query-String-Values-In-JavaScript.htm

All that being said, a simple google search would've gotten you this information.
Copy linkTweet thisAlerts:
@JunkMaleNov 17.2011 — Good point as the OP didn't say what the data source was from, I assumed it was a string and not the window.

This is rough working code, it will return an object of elements in a URL string.

[CODE]function getURLdata(url){
url = url.split("?");
url = url.pop();
obj = new Object;
// we will break at the &
url = url.split("&");
for(u in url){
url[u] = url[u].split("="); // parse the array and split the data
obj[ url[u][0] ] = url[u][1]; // now make an object.
}
return obj;
}[/CODE]


When I do [CODE]x = getURLdata("http://localhost/mywebsite/messaging.php?sub=hi&recip=Admin");[/CODE] and I then call [CODE]alert( x.recip );[/CODE] I get the expected result "Admin" but when I use x.sub or even trying x.xsub in the URL to see if this was a .sub key word issue but I still get the result "undefined" and I am stumped as to why because the logic of the routine is very simple and I expect the same would happen if you password window.location.search(1) to the function.
Copy linkTweet thisAlerts:
@aj_nscNov 18.2011 — Works fine for me:

http://jsbin.com/ocitih

new Object is a slow way to create an empty object in JS, though. You should just use the following:

<i>
</i>obj = {};
×

Success!

Help @tom90 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...