Javascript 在 Firefox 中工作正常,但在 Chrome 中给出 NaN
我正在努力帮助我的一个朋友,他必须对人们进行有关弹出窗口的测试(她研究通信科学)。所以基本上我们有 2 个网站,每个网站都有不同的弹出窗口。她问我是否可以创建一个每分钟显示一个弹出窗口的东西。但会议记录必须在多个页面上工作(例如,它从索引开始,但当我浏览网站并且分钟结束时,无论打开网站的哪个页面,都必须显示弹出窗口)。
我的javascript有点生疏了,因为自从我接触此事以来已经是3年前的事了。因此,对于这个问题,我使用了一些旧的 javascript 作业,我想我可以使用 cookies 来解决这个问题。我在使用全局变量时遇到了一些问题。我希望该网站可以在 firefox/chrome/IE 上运行,因为她必须在大学计算机上对其进行测试,并且我知道他们在那里使用哪种浏览器。
所以基本上console.log(“timeRemaining”)将在firefox(使用firebug)中显示正确的倒计时数字,但在chrome记录器(使用inspect元素)中它将显示NaN。因此,div 将不会显示(由于弹出窗口阻止程序,我不使用新窗口)。
问题可能出在哪里?我已经确保从 cookie 返回的值正在使用 parseInt 进行解析。我什至不明白为什么这个简单的代码在 Chrome 中返回的结果与在 Firefox 中返回的结果不同。
function setCookie(name, value) {
var str = name + "=" +escape(value);
str += "; expires= Tue, 28 Dec 2013 00:00:00";
document.cookie = str;
}
function getCookie(name) {
var allCookies = document.cookie.split("; "); // spatie!
for(i in allCookies) {
var singleCookie = allCookies[i].split("=");
if(singleCookie[0] == name) {
return unescape(singleCookie[1]);
}
}
return undefined;
}
function showLayer(layerId){
console.log("showlayer");
}
function checkTime() {
timeRemaining = timeRemaining - 1;
console.log(timeRemaining);
if(timeRemaining == 0) {
showLayer("popup");
timeRemaining = stdTijd;
}
setCookie("tijd", timeRemaining);
}
function loaders() {
timeRemaining = parseInt(getCookie("tijd"));
if(timeRemaining == undefined) {
console.log("new cookie created");
setCookie("tijd", stdTijd);
timeRemaining = parseInt(getCookie("tijd"));
}
console.log("cookie set, timeremaining=" + timeRemaining);
setInterval(checkTime, 1000);
}
var timeRemaining;
var stdTijd = 10;
if (document.getElementById) window.onload = loaders;
Console.log(timeRemaining) 上给出 NaN;在函数 checkTime.
Firefox 打印:
loaded, display = none
cookie set, timeremaining=10
9
8
7
..
Chrome 打印:
loaded, display = none
cookie set, timeremaining=NaN
NaN
NaN
..
提前致谢。
编辑:删除我的 cookie 后,我在 Firefox 中也得到 NaN 。所以问题不仅仅是 Firefox 和 chrome 之间的差异,而是我的代码中的一个基本错误。
编辑2:
I'm trying to help a friend of mine who has to do a test on people about popups (She studies communication sciences). So basically we have 2 websites with each a different popup. She asked me if I could create something that shows a popup every minute. But the minutes must work over several pages (ex. it starts on the index but when i browse around the website and the minute is over the popup has to show, no matter which page from the website is open).
My javascript is a bit rusty because it has been 3 years ago since I was in contact with the matter. So for the problem I used some of my old javascript assignments and I thought I would solve the problem using cookies. It's in the use of a global variable that I ran against some problems. I want the website to work on firefox/chrome/IE because she has to test it on the university computers and I have on idea which browser they are using over there.
So basically the console.log("timeRemaining") will show the correct number countdown in firefox (using firebug) but in the chrome logger (using inspect element) it will show NaN. Therefor the div will not be shown (I don't make use of a new windows because of popup blockers).
Where could be the problem? I allready made sure that the value returned from the cookie was being parsed using parseInt. I don't even understand why this simple code would return different results in Chrome than in Firefox.
function setCookie(name, value) {
var str = name + "=" +escape(value);
str += "; expires= Tue, 28 Dec 2013 00:00:00";
document.cookie = str;
}
function getCookie(name) {
var allCookies = document.cookie.split("; "); // spatie!
for(i in allCookies) {
var singleCookie = allCookies[i].split("=");
if(singleCookie[0] == name) {
return unescape(singleCookie[1]);
}
}
return undefined;
}
function showLayer(layerId){
console.log("showlayer");
}
function checkTime() {
timeRemaining = timeRemaining - 1;
console.log(timeRemaining);
if(timeRemaining == 0) {
showLayer("popup");
timeRemaining = stdTijd;
}
setCookie("tijd", timeRemaining);
}
function loaders() {
timeRemaining = parseInt(getCookie("tijd"));
if(timeRemaining == undefined) {
console.log("new cookie created");
setCookie("tijd", stdTijd);
timeRemaining = parseInt(getCookie("tijd"));
}
console.log("cookie set, timeremaining=" + timeRemaining);
setInterval(checkTime, 1000);
}
var timeRemaining;
var stdTijd = 10;
if (document.getElementById) window.onload = loaders;
The NaN is giving on the console.log(timeRemaining); in the function checkTime.
Firefox prints:
loaded, display = none
cookie set, timeremaining=10
9
8
7
..
Chrome prints:
loaded, display = none
cookie set, timeremaining=NaN
NaN
NaN
..
Thanks in advance.
EDIT: after deleting my cookies I get a NaN in firefox too. So the problem isn't just a difference between firefox and chrome instead it's a fundamental error in my code.
EDIT2:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取 cookie 时出现问题,这里有一个正在工作的小提琴: jsFiddle
在加载器中你正在做:
timeRemaining = parseInt(getCookie("timeRemaining"));
它应该是:
timeRemaining = parseInt(getCookie("tijd"));
另外,检查 cookie 返回的数据需要检查是否为 NaN,因为 get cookie 将返回 undefined,而 parseInt 将返回 NaN:
if(剩余时间==NaN)
There was a problem in getting the cookie back out, here is a fiddle that is working: jsFiddle
In loaders you were doing:
timeRemaining = parseInt(getCookie("timeRemaining"));
It should be:
timeRemaining = parseInt(getCookie("tijd"));
Also the check for the data back from the cookie needs to check if NaN, as the get cookie will send back undefined and parseInt will give back NaN:
if(timeRemaining==NaN)
timeRemaining
最初是未定义
,这就是您传递给setCookie
的内容。我想如果你编辑以下内容,它可能会起作用。到:
timeRemaining
is initiallyundefined
, and this is what you're passing tosetCookie
. I think if you edit the following, it might work.To: