2

I have to split values from href link.

my href link as follows

var val =location.href      // outputs http://localhost:8080/index.html?username=test@gmail.com&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 test@gmail.com and 68

Psl
  • 3,830
  • 16
  • 46
  • 84

9 Answers9

2

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.

1

For the "username" parameter :

url.split("username=")[1].split("&")[0] returns "test@gmail.com".

Same for "joid" :

url.split("joid=")[1] returns "68".

Edit: see this answer for a more reliable way of doing this.

Community
  • 1
  • 1
1

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

Community
  • 1
  • 1
sudhansu63
  • 6,025
  • 4
  • 39
  • 52
1

You can use following :

var url = "http://localhost:8080/index.html?username=test@gmail.com&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: "test@gmail.com", joid: "68"} 

Here is the demo : http://jsfiddle.net/uEqZs/

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
1
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".
coder
  • 13,002
  • 31
  • 112
  • 214
1

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

Vahid Hallaji
  • 7,159
  • 5
  • 42
  • 51
1

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

index.html?names=priya@gmail.com&id=68

use this function as

var email = getParameterByName("names");
var id = getParameterByName("id");

values would be

email = "priya@gmail.com";
id = "68";
Nirmal
  • 924
  • 4
  • 9
1

I think this might help

var loc_name = "http://localhost:8080/index.html?username=test@gmail.com&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

test@gmail.com
68 
test
0

Try

var id=val .match(/username=(.*?)(?=(&|$))/)[1]

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531