The image above presents how will the variables passed in the link. In this article I’m going to present how to get a URL parameter with JavaScript. Not only do we grab parameter values but we also modify them dynamically with the History API, so these […] Query string parameters have been incredibly useful on the server side since the internet took liftoff, but it wasn’t until AJAX-driven web apps became popular that we relied too much on them on the client side.
Get URL Parameter Value By Name Value of a given parameter name can be read through the get() method of URLSearchParams object. Hope it will help others:this question has too many answers, so i'm adding another one.this copes with empty parameters (those keys present without There are some good answers already, but I found them needlessly complex and hard to understand. Improvements: Use @Leif suggestion worked great. But it's in jQuery.
It basically reads the current page url, perform some regular expression on the URL then saves the url parameters in an associative array, which we can easily access.So as an example if we had the following url with the javascript at the bottom in place.All we’d need to do to get the parameters id and page are to call this:I rewrote it to vanilla JS and made a few improvements :To subscribe to this RSS feed, copy and paste this URL into your RSS reader. The code may seem clever at first glance, but isn't very well thought out. @ZiTAL This function is to be used with the query part of a URL, not the entire URL. It works properly if you put the regex into a variable outside of the loop. @Kafoso There is no clear right or wrong here. The version using regular expresion below is works better. It sounds like an application-specific thing that should be applied along with, or on top of query parameter extraction.I like this option best, but prefer to return null, or the result, but not an empty string.It looks like you have some extra escape chars. Leaving the function like this What about URL-decoding the parameter names and values? Please, make sure that your answer contributes information that is not among existing answers. I was getting in an ID 142# because at the end of the URL was the # generated on a button click. I agree with you. If this is solid, then something like this should be made into the standard library. For a list of browser support It's currently supported in Chrome, Firefox, Safari, Edge, and Here is a recursive solution that has no regex, and has minimal mutation (only the params object is mutated, which I believe is unavoidable in JS).Update 5/26/2017, here is an ES7 implementation (runs with babel preset stage 0, 1, 2, or 3):Update 3/26/2018, here is a Typescript implementation:Update 2/13/2019, here is an updated TypeScript implementation that works with TypeScript 3.Here is my solution. Featured on Meta This limit applies to both POST request and GET request URLs. Great answer!I think it would be handy for other a check for null was included. First of all, repeated fields with the same names have no specified standard behavior, and is up to the parser to handle. FYI, because it's a Map, usage is params.get("key");I like it but probably it won't get the first param. The Overflow Blog low level stuff ilke this shouldn't be reinvented over and over, nor left to developers to utilize whatever entropically variant solutions they will whip up. @Harvey Case insensitivity is not a concern of query parameters. Thanks for pointing that out! Additionally, the Look at that, two lines, does exactly what it says on the tin - and won't break if someone is trying to probe for vulnerabilities and adds a bunch of extra characters and queries unlike a couple of these solutions. "\\[" should be "\[". Your answer is currently flagged "low quality" and might eventually be removed without.I found that if there has a string like '#id' at the end of the url,and return the last parameter value like 'somevalue#id'@Fancyoung Yes,you are right,i made a mistake that use Tested both variants. Please, make sure that your answer contributes information that is not among existing answers.parse_query_string will fail for values that contain '=', like when passing a base64 encoded value as value. Javascript does not provide a core method to do that, so how to do it? BTW, Safari is the modern days' IE6Updated answer. Free 30 Day Trial
Get URL Parameter Value By Name Value of a given parameter name can be read through the get() method of URLSearchParams object. Hope it will help others:this question has too many answers, so i'm adding another one.this copes with empty parameters (those keys present without There are some good answers already, but I found them needlessly complex and hard to understand. Improvements: Use @Leif suggestion worked great. But it's in jQuery.
It basically reads the current page url, perform some regular expression on the URL then saves the url parameters in an associative array, which we can easily access.So as an example if we had the following url with the javascript at the bottom in place.All we’d need to do to get the parameters id and page are to call this:I rewrote it to vanilla JS and made a few improvements :To subscribe to this RSS feed, copy and paste this URL into your RSS reader. The code may seem clever at first glance, but isn't very well thought out. @ZiTAL This function is to be used with the query part of a URL, not the entire URL. It works properly if you put the regex into a variable outside of the loop. @Kafoso There is no clear right or wrong here. The version using regular expresion below is works better. It sounds like an application-specific thing that should be applied along with, or on top of query parameter extraction.I like this option best, but prefer to return null, or the result, but not an empty string.It looks like you have some extra escape chars. Leaving the function like this What about URL-decoding the parameter names and values? Please, make sure that your answer contributes information that is not among existing answers. I was getting in an ID 142# because at the end of the URL was the # generated on a button click. I agree with you. If this is solid, then something like this should be made into the standard library. For a list of browser support It's currently supported in Chrome, Firefox, Safari, Edge, and Here is a recursive solution that has no regex, and has minimal mutation (only the params object is mutated, which I believe is unavoidable in JS).Update 5/26/2017, here is an ES7 implementation (runs with babel preset stage 0, 1, 2, or 3):Update 3/26/2018, here is a Typescript implementation:Update 2/13/2019, here is an updated TypeScript implementation that works with TypeScript 3.Here is my solution. Featured on Meta This limit applies to both POST request and GET request URLs. Great answer!I think it would be handy for other a check for null was included. First of all, repeated fields with the same names have no specified standard behavior, and is up to the parser to handle. FYI, because it's a Map, usage is params.get("key");I like it but probably it won't get the first param. The Overflow Blog low level stuff ilke this shouldn't be reinvented over and over, nor left to developers to utilize whatever entropically variant solutions they will whip up. @Harvey Case insensitivity is not a concern of query parameters. Thanks for pointing that out! Additionally, the Look at that, two lines, does exactly what it says on the tin - and won't break if someone is trying to probe for vulnerabilities and adds a bunch of extra characters and queries unlike a couple of these solutions. "\\[" should be "\[". Your answer is currently flagged "low quality" and might eventually be removed without.I found that if there has a string like '#id' at the end of the url,and return the last parameter value like 'somevalue#id'@Fancyoung Yes,you are right,i made a mistake that use Tested both variants. Please, make sure that your answer contributes information that is not among existing answers.parse_query_string will fail for values that contain '=', like when passing a base64 encoded value as value. Javascript does not provide a core method to do that, so how to do it? BTW, Safari is the modern days' IE6Updated answer. Free 30 Day Trial