Javascript – How to get parameters on client side passsed by HTTP GET request

asp.net-mvcasp.net-mvc-4callbackjavascriptjquery

I need to catch a callback in my razor view page from some social service API.
The callback request is implemented by HTTP GET method:

http://www.contactsimporter.com/home.cshtml

After callback is implemented I need to retrieve parameters(param1=value1&param2=value2) from HTTP GET request.
For example:

http://www.contactsimporter.com/home.cshtml?param1=value1&param2=value2

I need to retrieve this parameters param1=value1&param2=value2 from callback URL.

Any idea or code example how can I get those parameters using JavaScript or jQuery code?
Thank you in advance.

Best Answer

Adapted from CSS-Tricks

  • To access to the url: window.location.pathname

Now you can treat the url like a normal string in Javascript and parse it accordingly.

var url = window.location.pathname
var getQuery = url.split('?')[1]
var params = getQuery.split('&') 
// params is ['param1=value', 'param2=value2']