javascript事件永远不会触发

时间:2011-06-28 17:56:19

标签: javascript events javascript-events refresh onbeforeunload

我正在进行邮件列表的注册,并尝试使浏览器不缓存页面和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"));
}

1 个答案:

答案 0 :(得分:0)

将此广告提交给您的主管部分

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">

指令CACHE-CONTROL:NO-CACHE表示不应使用缓存信息,而是应将请求转发到源服务器。