Pass JavaScript variable through url

I have a page that sets several variables using javascript Math.random()

as shown.

var RandomValue1 = Math.floor(Math.random() * 5);
var RandomValue2 = Math.floor(Math.random() * 6);
var string = array1[RandomValue1];
var string = array2[RandomValue2];

      

I would like to allow the user to pass the value RandomValue

through to URL

another person. When the second person goes into general URL

, RandomValue

it is meaning like the first person, so that the second person sees the same "line" as the first person.

I'm new to web design and web programming, so I don't know if this needs to be handled server-side or client-side.

I guess I need to pass url something like page1.html?RandomValue1=1&RandomValue2=3

, but I can't get the page to set these variables and load. Can this be done in JavaScript

?

+3


source to share


4 answers


You can read GET variables using javascript. Here is an example function ( source ):

function getUrlVars() {
  var vars = {};
  var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
    vars[key] = value;
  });
  return vars;
}

      



Using:

var first = getUrlVars()["id"];
var second = getUrlVars()["page"];

alert(first);
alert(second);

      

+2


source


Your guess is correct. You need to put these variables in the URL as PARAMETERS. I would suggest using "#" instead of # actual parameters, so you don't risk refreshing the page every time, so the url will look like mydomain.com/page.html#RandomValue1=1&RandomValue2=3.

You can use Javascript to get / set these parameters using window.location.href method. On page load, Javascript will check if there is a "#" and parse anything after it in the url. Once the page has loaded, Javascript can be used to update these parameters.

See the following link for complete documentation on how to use window.location:



https://developer.mozilla.org/en-US/docs/DOM/window.location

Oh, and here is a javascript library that can help you faster. It works with a hash ('#') and automatically parses (deparametrizes) the parameters for you, assigning them strings / objects, etc., which you can then use.

http://benalman.com/projects/jquery-bbq-plugin/

+1


source


I would do it in PHP.

You can put a hidden field on your page (in the form). Set the form's GET method and set the action to another PHP page.

<form id="form" name="form" action="my_page.php" method="GET">
  <input id="myId" name="myName" type="hidden />
</form>

      

In the PHP page, you can manipulate variables with the $ _GET object.

<?php
  if(isset($_GET['myName'])){
    //do something with value;
  }
?>

      

Just remember that the GET and POST variables are based on the name attribute from HTML elements. In my example, the hidden field has a name myName

, so when I want to refer to it I use$_GET['myName'];

0


source


You will need to use a server to coordinate data between both clients. In other words, the first user will have to send their random value to the server - this can be done with a GET url such as page1.html?RandomValue1=1

, although you may need to research POST requests.

In either case, the second user can navigate to the shared URL to see the value stored on the server.

0


source







All Articles