2

I have been trying to pass parameter in a ASP.NET page to another page (in our web site) by redirecting using JavaScript (or jQuery, Ajax, Fetch, etc.) and catch this parameter on Load event of the redirected page via JavaScript. However, I am not sure if I can do this. Any idea?

News Page:

window.location = '/news/details?id=4';

Details Page:

$(function (id) {
    console.log('Pass parameter: ' + id);        
});

Any help would be appreciated...

Jack
  • 1
  • 21
  • 118
  • 236

2 Answers2

3

On the Details page, use the URL object to get access to the url params, for example:

const url = new URL(window.location.href);
console.log(url.searchParams.get("id"));
Petr Broz
  • 8,891
  • 2
  • 15
  • 24
2

You can get the parameters from the url in your details page with the following code:

var url = new URL(window.location);
var param = url.searchParams.get("parameterName");

As @Michael Hurley points out, url is not available in some browsers (namely IE), so if you need to support them you'll have to do some additional work:

exp = /(?:parameterName=)(\w)/
match = str.match(exp)
//match[1] would hold the value of 'parameterName'
Velimir Tchatchevsky
  • 2,812
  • 1
  • 16
  • 21