0

I have an href link as follows.i have to spilt name value and id value separately.

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

Using jquery how can i split priya@gmail.com and 68 i tried the following one

var val =location.href.split('?')[1] which outputs the priya@gmail.com&id=68

i have to output priya@gmail.com and 68 separately..how it is possible..

Psl
  • 3,830
  • 16
  • 46
  • 84

4 Answers4

1
var str="priya@gmail.com&id=68";

var emailrogh= str.split("&");

email=emailrogh[0];// show priya@gmail.com

var idrough=emailrogh[1].split("=");

var id=idrough[1];//show 68

update

var str="name=priya@gmail.com&id=68";

str=str.split("name=")[1];

var emailrogh= str.split("&");

email=emailrogh[0];// show priya@gmail.com

var idrough=emailrogh[1].split("=");

var id=idrough[1];//show 68
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
  • var str="names=priya@gmail.com&id=68"; var str contains names also .it is not working – Psl Sep 26 '13 at 06:14
1

may be:

var str = "priya@gmail.com&id=68";
var splitted = str.replace("id=", "").split("&");
console.log( splitted );
//gives ["priya@gmail.com", "68"]
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
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";
Sohil Desai
  • 2,940
  • 5
  • 23
  • 35
1
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, " "));
}
Nirmal
  • 924
  • 4
  • 9