随机生成器创建自定义URL变量

时间:2015-06-29 16:59:39

标签: javascript variables url random

我正在尝试使用随机生成的附加变量生成自定义网址。

实施例: http://example.com/page1234.jsp?id=LOCATION_ID&user=USER_NAME&pass=PASSWORD

我很好,所有的变量数据都是数字的,并且一直试图用它来生成随机数:

<p>Click the button to 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>

我收到一些不错的大随机数,然后我尝试用它来附加网址:

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

URL函数主要用于将其推送到Window对象,然后转到该URL - 但是要插入var值会给我一个问题。

我不确定是不是因为它们没有嵌套,但是我得到了未定义的错误。最终我不需要/希望用户看到数字,如果页面加载并执行到脚本并加载了目标,那也没关系。我想看看HTML和控制台之间的输出差异,并且让按钮执行它有助于我看到步骤。

2 个答案:

答案 0 :(得分:0)

有一些问题。存储随机数的变量位于本地范围内 - 在定义它们的函数之外,它们是不可访问的。

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

你可以通过首先在全局范围内声明变量来解决这个问题:

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

请注意,如果在函数中使用var关键字,它将创建一个同名的新局部变量。

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

在将事物放在全局范围内时要小心,因为这些变量可以从页面上执行的任何脚本访问,并且有时可能会遇到您(或执行脚本的人)发现变量&#的情况。 39; s值意外地改变了。这可能不是出于您的目的所必需的,但我的偏好是在函数或对象中包装这样的东西,并创建一个伪类(Javascript还没有真正的类)。如果您想了解更多here's另一个答案,可以帮助您入门。

您遇到的第二个问题是您的genURL()功能。您的报价是不平衡的,您的变量是字符串的一部分。

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'

答案 1 :(得分:0)

好的,所以我想出来了......感谢@Grinde - 我添加了全局var调用,然后想出如何将它们注入到URL字符串中......

这是最终的脚本代码,地址已更改以保护无辜者......

var firstName;

function genFirst() {

    firstName = Math.floor((Math.random() * 1000000000000) + 13579);
    document.innerHTML = firstName;
        console.log(firstName);
}

var lastName;

function genLast() {

    lastName = Math.floor((Math.random() * 1000000000000) + 111);
    document.innerHTML = lastName;
        console.log(lastName);

}

var email;

function genEmail() {

    email = Math.floor((Math.random() * 1000000000000) + 10101);
    document.innerHTML = email;
        console.log(email);

}

function genURL() {

    document.action = window.location.href = "http://domain.com/log_thru.jsp?eid=12345&seid=101" + "&email=" + [email] + "@fakemailacct.com" "&first_name=" + [firstName] + "&last_name=" + [lastName]; 
            return false; 
}

function genID() {

    genFirst()
    genLast()
    genEmail()
}

genID() 

然后使用以下HTML调用它:

<!doctype html>

<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--> 

<html class="no-js" lang="en"> <!--<![endif]-->
<head>
    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    </head>
    <body>
        <script src="app2.js"></script>
        <p>Click the button to join the event.</p>

        <button onclick="genURL()">Join</button>
    </body>
</html>

感谢那些贡献并提供指导的人们!

相关问题