【发布时间】:2011-06-28 17:56:18
【问题描述】:
我正在注册邮件列表,并试图使页面和 js 文件不被浏览器缓存。因此,我使用了一个假的查询字符串。我试图让页面在刷新时创建一个新的查询字符串。它不起作用……为什么?我认为这条线:
window.onbeforeunload=changeNoCache();
放错地方了。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Email Sign-Up</title>
<link href="SignUp.css" rel="stylesheet" type="text/css" />
<script id="script" type="text/javascript" src="SignUp.js"></script>
<script type="text/javascript">
function noCache() {
regex=/noCache/;
if (!regex.test(window.location.href)) {
rand=getRandom();
window.location.href+="?noCache="+rand;
}
else {
h=true;
}
if (!regex.test(document.getElementById("script").src)) {
rand=getRandom();
document.getElementById("script").src+="?noCache="+rand;
}
else {
s=true;
}
if (h&&s) {
window.onbeforeunload=changeNoCache();
}
}
function changeNoCache() {
loc=window.location.href.indexOf("?");
window.location.href=window.location.href.substring(0,loc);
loc=document.getElementById("script").src.indexOf("?");
document.getElementbyId("script").src=document.getElementById("script").src.substring(0,loc);
noCache();
}
function getRandom() {
rand=Math.random()+"";
rand=rand.substring(0,15);
rand=Number(rand);
return rand;
}
</script>
</head>
<body onLoad="noCache();">
Email Address: <input type="email" id="email" />
<button onClick="makePopUp();">Submit</button>
</body>
</html>
SignUp.js:
function makePopUp() {
div = document.getElementById("shell");
if(!div) {
email = document.getElementById("email").value;
popupshell = document.createElement("div");
popupinner = document.createElement("div");
popupshell.id = "shell";
popupinner.id = "inner";
popupinner.innerHTML = "<span id=\"closebutton\" onClick=\"closePopUp();\">x</span><div id=\"form\">Thank You. Would you like to enter other information so that we can make your emails more relevant? If so, enter your information below. Otherwise, you may click the x button in the top right of this window to close it.<br /><br /><form name=\"Email Signup\" action=\"sql_submit.php\" method=\"post\"><input type=\"hidden\" value=\"Email Signup\" name=\"formname\" /><input type=\"hidden\" value=\""+window.location+"\" name=\"url\" />Email: <input name=\"email\" type=\"text\" value=\""+email+"\" /><br />First Name: <input type=\"text\" name=\"firstname\" /><br />Last Name: <input type=\"text\" name=\"lastname\" /><br />Country Code: <input type=\"text\" name=\"countrycode\" id=\"countrycode\" size=\"2\" onBlur=\"validatePhone();\" />Phone Number: <input type=\"text\" name=\"number\" id=\"number\" size=\"10\" onBlur=\"validatePhone();\" /><br />Company Name: <input type=\"text\" name=\"company\" /><br />Serial Number: <input type=\"text\" name=\"serial\" /><br /><br /><input type=\"submit\" value=\"Submit\" /></form></div>";
popupshell.appendChild(popupinner);
document.body.appendChild(popupshell);
}
else {
blink(div);
t = setTimeout("blink(div);",100);
t = setTimeout("blink(div);",200);
t = setTimeout("blink(div);",300);
t = setTimeout("blink(div);",400);
t = setTimeout("blink(div);",500);
}
}
function blink(element) {
(element.style.display=='none') ? element.style.display='block' : element.style.display='none';
}
function validatePhone() {
cc = document.getElementById("countrycode");
number = document.getElementById("number");
cc.value = cc.value.replace(/\D/g,"");
number.value = number.value.replace(/\D/g,"");
}
function closePopUp() {
document.body.removeChild(document.getElementById("shell"));
}
【问题讨论】:
-
我以前一直触发,现在根本不触发。
-
当它无休止地触发时,我把代码放在了一个糟糕的地方。现在可能是这样。
-
我在 javascript 中没有看到 makePopUp 方法,是否在另一个文件中,添加 alert('something');在 onclick 事件中的 makePopUp 之前
-
signup.js 是我用来消除弹出窗口的。如果你愿意,我会把它添加到帖子中。
-
@Senad Meškin:我的 onclick 事件没有问题。请阅读问题。
标签: javascript events refresh dom-events onbeforeunload