Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the GET parameter of a URL in JavaScript [duplicate]

If I am on a page such as

http://somesite.com/somepage.php?param1=asdf

In the JavaScript of that page, I would like to set a variable to the value of the parameter in the GET part of the URL.

So in JavaScript:

<script>    param1var = ...   // ... would be replaced with the code to get asdf from URI </script> 

What would "..." be?

like image 214
RobKohr Avatar asked May 05 '09 23:05

RobKohr


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.

How can I get multiple values from GET request 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 "&".

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.

What is get parameter in URL?

GET parameters (also called URL parameters or query strings) are used when a client, such as a browser, requests a particular resource from a web server using the HTTP protocol. These parameters are usually name-value pairs, separated by an equals sign = . They can be used for a variety of things, as explained below.


1 Answers

Here's some sample code for that.

<script> var param1var = getQueryVariable("param1");  function getQueryVariable(variable) {   var query = window.location.search.substring(1);   var vars = query.split("&");   for (var i=0;i<vars.length;i++) {     var pair = vars[i].split("=");     if (pair[0] == variable) {       return pair[1];     }   }    alert('Query Variable ' + variable + ' not found'); } </script> 
like image 193
Jose Basilio Avatar answered Sep 28 '22 05:09

Jose Basilio