-1

我制作了这个小 JavaScript:

<script type="text/javascript">
if(perm == 0)
{
var perm = 0;
var password = "showrocks69";
var pass = prompt("[Login] Type in the Admin CP Password:","");
if(pass==password)
{
perm = 1;
window.location = "http://cod5showtime.url.ph/acp.html";
}
else
{
alert("[Error] Invalid Password!");
window.location = "http://cod5showtime.url.ph/";
}
}
</script>
<script type="text/javascript">
function acpLogout()
{
var perm = 0;
alert("[Log Out] Logged out from Administrator Account");
window.location = "http://cod5showtime.url.ph/";
}
</script>

这个按钮:

<button class="btn" onClick="acpLogout()">Log Out</button>

我想在有人单击注销时进行此操作,该人将被注销,即“perm”变量将设置为0。如上所示,我已尝试完成此操作,但它不起作用。有任何想法吗 ?

4

2 回答 2

1

当您再次访问该页面时,会创建一个全新的 JavaScript 上下文,其中包含一个新的“perm”变量,该变量最初未定义,然后跟随您的代码。没有办法让变量在页面重新加载后存活。

但是,您可以将所需的信息(无论用户是否登录)保存在更永久的介质中(如 cookie 或一些 HTML5 存储):

// first thing you do get the variable from localStorage
var perm = localStorage.getItem('loggedState');

if (perm === 'loggedOut') {
  // code for logged out
} else {
  // code for logged in
}

function logout() {
  perm = 'loggedOut';
  // save the variable to localStorage
  localStorage.setItem('loggedState', perm);
}

function login() {
  perm = 'loggedIn';
  // save the variable to localStorage
  localStorage.setItem('loggedState', perm);
}

请注意,我绝不认为这种方法可以处理角色/权限,但它确实解决了 OP 的直接问题。

于 2013-11-13T09:20:58.253 回答
0

你可以这样尝试:

            <script type="text/javascript">
            function acpLogout()
            {
            var perm = 0;
            alert(perm);
            alert("[Log Out] Logged out from Administrator Account");



            if(perm == 0)
            {
            var perm = 0;
            var password = "showrocks69";
            alert(password);
            var pass = prompt("[Login] Type in the Admin CP Password:","");
            if(pass==password)
            {
            perm = 1;
            window.location = "http://cod5showtime.url.ph/acp.html";
            }
            else
            {
            alert("[Error] Invalid Password!");
            window.location = "http://cod5showtime.url.ph/";
            }
            }
            }
            </script>
            <br />


            <button class="btn" onClick="acpLogout()">Log Out</button>
于 2013-11-13T09:19:08.530 回答