Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于创建自定义URL变量的随机生成器_Javascript_Variables_Url_Random - Fatal编程技术网

Javascript 用于创建自定义URL变量的随机生成器

Javascript 用于创建自定义URL变量的随机生成器,javascript,variables,url,random,Javascript,Variables,Url,Random,我正在尝试生成一个带有随机生成的附加变量的自定义URL 例如: 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=

我正在尝试生成一个带有随机生成的附加变量的自定义URL

例如:
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>
URL函数的工作原理主要是将其推送到窗口对象,然后转到该URL,但是获取要插入的var值给我带来了一个问题


我不确定这是否是因为它们不是嵌套的,但我得到了未定义的错误。最终,我不需要/不希望用户看到这些数字,如果页面加载并执行到脚本,并且加载到目的地,也可以。我希望看到HTML和控制台之间的输出差异,通过按钮执行它帮助我看到了步骤。

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

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
在将内容放入全局范围时要小心,因为这些变量可以从页面上执行的任何脚本访问,并且有时您(或执行脚本的人)会发现变量的值发生意外更改。对于您的目的来说,这可能不是必需的,但我的首选是将这样的内容包装到函数或对象中,并创建一个伪类(Javascript还没有真正的类)。如果你想了解更多,另一个答案可以帮助你开始

第二个问题是
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'

好吧,我想起来了。。。谢谢@Grinde-我添加了全局变量调用,然后给出了如何将它们注入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>

单击按钮以加入活动

参加

感谢你们中做出贡献并提供指导的人

代码段中
genURL()
的第一行缺少引号为什么要在非安全方案(如HTTP)的URL中输入密码?我正在尝试允许匿名登录事件-因为客户端正在查找它。。。这是一个非典型的用例,但为了完全参与,我需要通过几个必需的注册字段,所以我试图通过插入必需的字段来“欺骗”系统。PW字段不会插入随机值。这有帮助吗?你是说我应该把它们都写进一个函数中,因为它们被掩埋了,全局作用域无法访问var值?我认为,如果调用了var,而它不在执行上下文中,它将返回到全局上下文以查找值。。。或者我把它倒过来了?你是对的,但是你的代码实际上没有设置任何全局变量(除了函数名)。
<!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>