使用jQuery在不同的网页上定义和调用变量?

时间:2014-11-26 17:10:46

标签: javascript jquery

我是JavaScript和这个网站的新手,我正在尝试创建一个用户名和密码创建者并登录。现在我只是使用预制的用户名和密码。我试过用JavaScript做这个,但我的代码在记事本++上不适合我。我已经学会了对jQuery的基本理解,这是让这个模拟网站运行的最后手段。你能告诉我是否有办法用JavaScript做到这一点,你是如何解释代码,使用jQuery在两个不同的网页上共享变量。请求用简单的英语解释,因为我说我是JavaScript的新手,也是jQuery的新手。

这里是定义用户名和密码变量的地方。

<!DOCTYPE html>
<html>
<head>

<title>
create account
</title>


<script>
sessionStorage.setItem( "username1", ["bob", "sam"]);
sessionStorage.setItem( "password1", ["lol", "jk"]);
</script>


</head>

<body>

</body>


</html>

这里是我称之为变量

的地方
<!DOCTYPE html>
<html>
<head>
<title>
log on page
</title>

<script type = "text/javascript">
var count = 2;

function validate() {
    var un = document.myform.username.value;
    var pw = document.myform.pword.value;
    var valid = false;

    var unArray = sessionStorage.getItem("username1");
    var unArray = sessionStorage.getItem("password1");

    for (var i=0; i <unArray.length; i++) {
        if ((un == unArray[i]) && (pw == pwArray[i])) {
        valid = true;
            break;
        }
    }

    if (valid) {
        alert ("Login was successful");
        window.location = "http://www.google.com";
        return false;
    }

    var t = " tries";

    if (count == 1) {t = " try"}

    if (count >= 1) {
        alert ("Invalid username and/or password. " +
               "You have " + count + t + " left.");
        document.myform.username.value = "";
        document.myform.pword.value = "";
        setTimeout("document.myform.username.focus()", 25);
        setTimeout("document.myform.username.select()", 25);
        count --;
    }

    else {
        alert ("Still incorrect! You have no more tries left!");
        document.myform.username.value = "No more tries allowed!";
        document.myform.pword.value = "";
        document.myform.username.disabled = true;
        document.myform.pword.disabled = true;
        return false;
    }

}
</script>
<!--this-->
    <style>

    p.log_on{
    position: fixed;
    top: 30px;
    left: 20px;
}
</style>





</head>


<body>

<!--here-->
<form name = "myform">
  <p class="log_on">
    ENTER USER NAME <input type="text" name="username"><br><br><br><br><br>
    ENTER PASSWORD <input type="password" name="pword">
    <input type="button" value="Check In" name="Submit" onclick="validate()">
  </p>
</form>
<!--to here-->

</body>


</html>

0 个答案:

没有答案