Cookie代码可在FireFox和Explorer中使用,但不能在Chrome中使用
Cookie code working in FireFox and Explorer but not Chrome
以下cookie制作代码适用于Firefox和Explorer,但不适用于Chrome。
我已经设置了警报来停止脚本,并验证新的Date()对象是否按预期创建。所有3个浏览器都指示正在创建新的Date(),并且在到期日前添加10分钟也正在按预期进行。
流程:登录页面-->页面
这是登录页面用于创建cookie的代码:
/*
Event handler for verify button click.
@param event is the event that triggered this function.
*/
function checkUser(event){
// interesting note: even though the button isn't of type submit, it submits the form.
// this prevents the form from being submitted:
event.preventDefault();
// obtain user name from form
var name = this.form.userName.value;
// obtain password from form
var password = this.form.password.value;
// is the password valid?
if(isValidPassword(password)){
// create new Date object for cookie's expiration
var expires = new Date();
if(expires){
alert(expires.toString());
}
// increase expire time by 10 minutes
expires.setMinutes(expires.getMinutes() + 10);
alert(expires); // Firefox & Chrome show with additional 10 minutes.
// write userName cookie
document.cookie = "userName=" + encodeURIComponent(name) +
"; expires=" + expires.toUTCString();
// write password cookie
document.cookie = "password=" +encodeURIComponent(password) +
"; expires=" + expires.toUTCString();
// password is valid, allow entry to private web page.
location.href = "project3.html";
}else{
// password was not valid
alert("Password must be at least 1 character in length.");
this.form.password.focus();
}
}
然后在location.href"project3.html"中,这是检查cookie的代码。警报(allCookies)在Firefox和Explorer中弹出并显示Cookie,但在Chrome中完全为空所以我会立即被重定向回登录页面。这似乎表明要么我的cookie编写代码不工作,要么cookie在Chrome中被关闭?但我检查了Chrome浏览器,并选择了Cookie内容设置下的"允许设置本地数据"单选按钮。这让我觉得我在制作cookie时做错了什么,Chrome不喜欢,你能发现它并告诉我问题出在哪里吗?非常感谢。
//
// Ensure user has a right to visit this web page.
//
//attach load event listener to window
window.addEventListener('load', verifyPassword, false);
// attach blur event listener to window
window.addEventListener('blur', verifyPassword, false);
/*
Event handler for window load event. Ensures user has proper credentials
via looking for password name:value pair held in document.cookie collection.
*/
function verifyPassword(){
// decode cookies
var allCookies = decodeURIComponent(document.cookie);
alert(allCookies); // Blank in Chrome; shows cookies in Firefox and Explorer.
var password;
// is the password cookie present?
if(hasCookie("password", allCookies)){
// password cookie present, get password value
password = getCookie("password", allCookies);
}else{
// send user to login page
location.href = "project3_login.html";
}
// password is present, is it valid?
if(isValidPassword(password)){
// they get to stay
// cookie expires in...
}else{
// send user to login page
location.href = "project3_login.html";
}
}
我是javascript的新手。我学习了饼干&我的头被铬打破了。我的代码在IE上运行良好,但在Chrome上则不然。Chrome表示已启用cookie。但是,我写不出来。版本38.0.2125.111如果你在chrome上进入"about",你可以得到帮助。显然这是一个问题。chrome以不同的方式处理cookie。我不明白。我还没学那么多。希望我的指针能有所帮助。
相关文章:
- JS可以在Chrome中工作,但不能在Firefox中工作
- WebRTC视频聊天可以在FF中使用,但不能在Chrome中使用
- jQuery Datepicker可以在Safari中工作,但不能在FF或Chrome中工作
- 三角库可以与firefox一起使用,但不能在Chrome中使用
- Json到CSV下载,可以在chrome中工作,但不能在IE浏览器中工作
- jQuery$.post可以在chrome、safari中工作,但不能在FF中工作(声明成功回调函数未定义)
- Javascript可以在chrome中使用,但不能在其他浏览器中使用
- Iframe加载调整大小在Chrome中工作,但不能在IE或Firefox中工作
- jQuery/Javascript函数可以在Chrome中使用,但不能在IE11中使用
- 在Firefox中工作正常,但在Chrome中不能
- 如何在HTML / CSS中填充垂直和滚动中间窗格(Chrome可以工作,但不能Firefox)
- RGB 到 HEX JavaScript 函数在 Chrome 中工作,但不能在 Firefox 或 Safari 中工
- 我的简单Javascript代码可以在Safari上运行,但不能在Chrome、Firefox或Opera上运行
- d3实现可以在firefox/chrome上运行,但不能在iPad上运行
- JavaScript可以在IE+Chrome中使用,但不能在FF中使用
- Javascript在Firefox中工作,但在Chrome中不能
- .load() 适用于 IE 和 dreamweaver 预览版,但不能使用 opera 和 chrome
- JQuery CSS Transform在Chrome和Firefox中工作,但不能在IE中工作
- 简单的jQuery在Firefox中工作,但不能在Chrome中工作
- Javascript在Safari中工作,但不能在Chrome或Firefox中工作