-3

I am fairly new to html and js. Let say if I have the following codes in index.html:

<a href='edit.html?id=1>item1</a>
<a href='edit.html?id=2>item2</a>
<a href='edit.html?id=3>item3</a>

then I create a page named edit.html But How do I keep track of the id(1 or 2 or 3) that is being passed to edit.html using jquery or js?

Please help..

qwr qwr
  • 1,049
  • 4
  • 22
  • 46
  • 2
    Potential duplicate of http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript – Neverever Jun 27 '12 at 03:28

3 Answers3

1
function getId() {
    var href = window.location.href,
        id = href.substr( href.indexOf('?') + 1 ).replace('id=',''); // 1, 2 , 3..
    return id;
}
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
1

The parameters you are passing are call the query string. See this answer on stack overflow for how to retrieve them

Code:

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null) 
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

Credit:

How can I get query string values in JavaScript?

Community
  • 1
  • 1
secretformula
  • 6,414
  • 3
  • 33
  • 56
1

Do you want to get the query string with jQuery on the page edit.html?

See: How can I get query string values in JavaScript?

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

on edit.html: getParameterByName('id');

Community
  • 1
  • 1
Lusitanian
  • 11,012
  • 1
  • 41
  • 38
  • Hi, could you explain more? So are you saying i should have a onload call on edit.html and inside the onload method call the getParameterByName('id')? – qwr qwr Jun 27 '12 at 03:53
  • If that's what you want. Store the result of the function in a variable. – Lusitanian Jun 27 '12 at 03:54