javascript - Random Generator to create custom URL variables -


i trying generate custom url appended variables randomly generated.

example: http://example.com/page1234.jsp?id=location_id&user=user_name&pass=password

i'm fine of variable data being numeric, , have been trying use generate random numbers:

<p>click button join.</p>  <button onclick="genid5()">join</button>  <p id="generateid1"></p><p id="generateid2"></p><p id="generateid3"></p>  <script> function genida() {     var x = math.floor((math.random() * 1000000000000) + 101);     document.getelementbyid("generateid1").innerhtml = x;         console.log(x); }  function genidb() {     var y = math.floor((math.random() * 1000000000000) + 101);     document.getelementbyid("generateid2").innerhtml = y;         console.log(y); }  function genidc() {     var z = math.floor((math.random() * 1000000000000) + 101);     document.getelementbyid("generateid3").innerhtml = z;         console.log(z); }  function genid5() {     genida();     genidb();     genidc();  }   </script> 

i getting nice big random numbers, , trying use append url:

function genurl() {          document.action = window.location.href = "http://example.com/page1234.jsp?id=" + x + &user= y + &pass= + z;              return true;  } 

the url function works far pushing window object, , going url - getting var values insert giving me problem.

i not sure if because not nested, getting undefined errors. don't need/want user see numbers, , if page loaded , executed script , loaded destination, ok well. wanted see output difference between html , console, , having button execute helped me see steps.

there few problems. variables storing random numbers in local scope - outside of functions define them not accessible.

function setvar() {    var foo = 'bar'; } setvar(); console.log( foo ); // undefined 

you can around first declaring variable in global scope so:

var foo; function setvar() {    foo = 'bar'; } setvar(); console.log( foo ); // 'bar' 

note if use var keyword in function create new local variable of same name.

var foo; function setvar() {    var foo = 'bar'; } setvar(); console.log( foo ); // undefined 

you want careful when putting things in global scope, variables accessible script executing on page, , can run situations (or executing script) find variable's value has unexpectedly changed. may not necessary purposes, it's preference wrap things in function or object, , create pseudo class (javascript doesn't have true classes yet). if want learn more here's answer started.

the second problem have genurl() function. quotes imbalanced, , variables part of string.

var x = 1, y = 2, z = 3;  console.log("variables: x + , + y + , + z"); // 'variables: x + , + y + , + z'  console.log("variables: " + x + "," + y + "," + z); // 'variables: 1,2,3' 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -