0

I am trying to get the id from an url that looks like this '#somepage?id=5' the function I am using for this doesn't seem to work the function in question is:

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

is there a way I can modify or replace this function to pull the id from a url as stated above?

Kern Elliott
  • 1,659
  • 5
  • 41
  • 65

1 Answers1

0
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, " "));
}

Javascript code reference

Community
  • 1
  • 1
Amit
  • 15,217
  • 8
  • 46
  • 68
  • Note however that your snippet will have the same issue as the OP. the data is stored in the Hash, not the querystring. – Kevin B May 20 '13 at 15:33