需要帮助javascript无法正常工作

时间:2015-07-11 20:02:26

标签: javascript

http://jsfiddle.net/Chinni123/qpqobso8/1/

在点击卡上它应该验证用户是否不在getcookie中然后它应该返回false并覆盖ahref并转到Cardspass.htm.This not overriding

document.getElementById("myLink1").onclick = function () {
     var user = getCookie("username");
     alert(user);
     if (user === "") {
         alert("no user");
         return true;
     } else {


    alert("we user");
         window.frames.content.location = "/cards/Cardspass.htm";
         return false;
     }
 };


 function getCookie(cname) {
     var name = cname + "=";
     var ca = document.cookie.split(';');
     for (var i = 0; i < ca.length; i++) {
         var c = ca[i];
         while (c.charAt(0) == ' ') c = c.substring(1);
         if (c.indexOf(name) === 0) {
             return c.substring(name.length, c.length);
         }
     }
     return "";
 }

我是jsfiddle和javascript的新手。

1 个答案:

答案 0 :(得分:0)

你必须使用cookies吗?如果没有,为什么不使用本地存储:

     document.getElementById("myLink1").onclick = function () {
         var user = localStorage.getItem("username");
         alert(user);
         if (!user) {
             alert("no user");
             location.href = "/cards/Cards.htm";
         } else {
             alert("we user");
             location.href = "/cards/Cardspass.htm";
         }
     };

并设置键值对,无论你在哪里:

localStorage.setItem("username", dataString);