Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get URL parameters with Javascript? [duplicate]

Tags:

javascript

Possible Duplicate:
How to get “GET” request parameters in JavaScript?
jQuery querystring
How can I get query string values in JavaScript?

Javascript does not provide a core method to do that, so how to do it?

like image 728
David Morales Avatar asked Jul 20 '12 15:07

David Morales


People also ask

How do I copy a URL parameter?

Answer : You can control if the Source URL parameters are copied to the Destination URL. The Site Preference is : Business Manager > site > Merchant Tools > Site Preferences > Storefront URLs > Copy Source Parameters.

Can you use Javascript to get URL parameter values?

The short answer is yes Javascript can parse URL parameter values. You can do this by leveraging URL Parameters to: Pass values from one page to another using the Javascript Get Method. Pass custom values to Google Analytics using the Google Tag Manager URL Variable which works the same as using a Javascript function.

How do you get multiple parameters in a URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

How do I find the parameter of a URL?

For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.


1 Answers

function getURLParameter(name) {   return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null; } 

So you can use:

myvar = getURLParameter('myvar'); 
like image 133
David Morales Avatar answered Sep 29 '22 04:09

David Morales