2015-07-11 40 views
-1

http://jsfiddle.net/Chinni123/qpqobso8/1/需要帮助的JavaScript不点击卡工作

应该验证,如果用户的getCookie不存在,那么它应该返回false并覆盖AHREF和去Cardspass.htm.This未覆盖

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的新手。

+0

谢谢@ danleyb2。内容是帧兄弟帧。所以,我想我需要提及。和我试图与window.location不起作用 – chinni

回答

0

您必须使用Cookie吗?如果不是,为什么不使用本地存储:

 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); 
+0

谢谢@格雷厄姆吨我的问题是cardspass.htm由于window.frames.content.location不显示在“内容”框架。饼干工作正常。它不会返回false(即使alert“我们的用户被显示”)并覆盖ahref中的cards.htm。 – chinni

+0

在小提琴中发布你的帧HTML –

+0

http://jsfiddle.net/Chinni123/txyprL6r/ – chinni