1

I've written the following codes on page 1 :

location.href = '@Url.Action("Page2", "Home")?MyDetail=' + age;

Upon clicking "Submit" button, it'll go to Page2 and the link bar will display:

"http://localhost:1234/Home/Page2?MyDetail=20"

Question is, on page 2 of my view level, how can I "request query string" to get the parameter value (i.e. age) from the link by using javascript?

Other questions I've searched used certain regular expressions which I didn't understand but I was wondering if there is another method. If regular expressions need to be used, I'd appreciate if someone could explain how's it working.

lalalaa
  • 21
  • 1
  • 4
  • asp.net: To get a querystring you can use the `Request.Querystring property`: `Request.Querystring("parameter1")` and for javascript you could use this:[/how-can-i-get-query-string-values-in-javascript](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Kevin Kloet Nov 14 '16 at 09:01
  • @KevinKloet Thanks for the comment! Yes, Request.Querystring is the one I know of, but I'm stuck on how to use it the javascript way. The link provided is the one I managed to search for but I don't understand how it is used. – lalalaa Nov 14 '16 at 09:14
  • what part of it do you not understand? the regex? the returns? – Kevin Kloet Nov 14 '16 at 09:21
  • @KevinKloet Like, why the reply (name.replace), what those that they are replacing means, about the regex and what are the returns based on? The whole thing actually.. – lalalaa Nov 14 '16 at 09:30
  • pretty hard to read `function getParameterByName(name, url){ if (!url){ /*if url param is passed*/ url = window.location.href; /*get current url*/ } name = name.replace(/[\[\]]/g, "\\$&"); /*replace any escaped characters*/ var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)"), results = regex.exec(url); /*name after ? or &,match anything that is not a & or #, or match & or # or $.*/ if (!results) return null; /*if result is undefined */if (!results[2]) return ''; /*if results[2] is undefined*/ return decodeURIComponent(results[2].replace(/\+/g, " ")); //decode and replace +}` – Kevin Kloet Nov 14 '16 at 09:50

1 Answers1

3

In C#:

string parameter = Request.QueryString["MyDetail"].ToString();

In Javascript:

var url = window.location.href; 
var split = url.split("MyDetail="); 
var currentURL = split[1];
James Haug
  • 1,426
  • 12
  • 27
  • Is there a way for me to use javascript instead? – lalalaa Nov 14 '16 at 09:15
  • u can use jquery...I will give u one example – Shajeer Puzhakkal Nov 14 '16 at 09:16
  • var url = window.location.href; var split = url.split("MyDetail="); var currentURL = split[1]; – Shajeer Puzhakkal Nov 14 '16 at 09:19
  • @shajeerpuzhakkal your answer has no jQuery in it... that is pure JavaScript. – Ant P Nov 14 '16 at 09:35
  • @shajeerpuzhakkal could you explain what the url.split("MyDetail=") does? And what the split[1] represents / what currentURL would contain? – lalalaa Nov 14 '16 at 09:44
  • `var split = url.split(MyDetail=)` splits the string url at that point, `split[0]` equals the part before the match of the `MyDetail=` and `split[1]` matches the first part after the `MyDetail=` in case you have multiple times `MyDetail=` in the url string you could specify which one you want to get. read more here: [javascript split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) – Kevin Kloet Nov 14 '16 at 09:54
  • Here our requirement is to get the ID.So I splite the URL "http://localhost:1234/Home/Page2?MyDetail=20"" with MyDetail.If I am spliting with any other thing instead of MyDetail (ex:Splitting with '/' then array size will increase.).If you have more doubt try it in your machine. – Shajeer Puzhakkal Nov 14 '16 at 10:04