Reputation: 3920
I have to split values from href link.
my href link as follows
var val =location.href // outputs http://localhost:8080/[email protected]&joid=68
i want to get username
value and joid
value separately.
I tried following but not working
var emailrogh= val.split("&");
email=emailrogh[0];
var idrough=emailrogh[1].split("=");
var id=idrough[1];
how can i extract [email protected]
and 68
Upvotes: 2
Views: 985
Reputation: 44
I think this might help
var loc_name = "http://localhost:8080/[email protected]&joid=68&test=test";
var get_params = (loc_name.split("?")[1]).split("&");
var contents = [];
for(var i= 0; i < get_params.length ; i++){
contents[(get_params[i].split("=")[0])] = get_params[i].split("=")[1];
}
console.log(contents['username']);
console.log(contents['joid']);
console.log(contents['test']);
This will parse the query string in to an array. I have got the following output in console
[email protected]
68
test
Upvotes: 1
Reputation: 651
You can use this method
var my_link = location.search.substring().split("&");
var email = my_link[0].split("=");
email = email[1];
var id = my_link[1].split("=");
id = id[1];
alert(email);
alert(id);
I hope this will work.
Upvotes: 2
Reputation: 7447
Try this small & clean function:
function getValue(name) {
var filter = new RegExp( name + "=([^&]+)" );
return unescape( window.location.match(filter)[1] );
}
var username = getValue("username");
var joid = getValue("joid");
See JsFiddle
Upvotes: 1
Reputation: 934
use this function
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
this function will return your value directly.
for eg. for your link
[email protected]&id=68
use this function as
var email = getParameterByName("names");
var id = getParameterByName("id");
values would be
email = "[email protected]";
id = "68";
Upvotes: 1
Reputation: 13248
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
And this is how you can use this function assuming the URL is,
"http://samplepage.com/?technology=jquery&blog=jquerybyexample".
Upvotes: 1
Reputation: 20830
You can use following :
var url = "http://localhost:8080/[email protected]&joid=68";
var vars = {}, key;
var querystring = url.slice(url.indexOf('?') + 1).split('&');
for(var i = 0; i < querystring.length; i++){
key = querystring[i].split('=');
vars[key[0]] = key[1];
}
console.log( vars);
Output would be :
Object {username: "[email protected]", joid: "68"}
Here is the demo : http://jsfiddle.net/uEqZs/
Upvotes: 1
Reputation: 6180
You first need to get the URl from the href attribute.
var wholeString = $("#MyAncId").attr('href');
then use the following method to get the Querystring value
function getParameterByName(wholeString, name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(wholeString);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Source:DextOr
Upvotes: 1
Reputation:
For the "username" parameter :
url.split("username=")[1].split("&")[0]
returns "[email protected]".
Same for "joid" :
url.split("joid=")[1]
returns "68".
Edit: see this answer for a more reliable way of doing this.
Upvotes: 1