【问题标题】:Javascript event never triggeringJavascript 事件从不触发
【发布时间】: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


【解决方案1】:

将此广告添加到您的头部

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

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

【讨论】:

  • @Senad Meškin 我知道这是一个选项,但出于多种原因我不想使用它。
  • @Senad Meškin 我希望我的 javascript 不缓存。
  • 就是这样,如果你添加指令缓存控制它不会使用缓存中的任何东西
  • @senad ive 也听说上面的代码只适用于某些浏览器。
  • 我不这么认为,这是规则,如果浏览器不遵守规则而不是真正的浏览器......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
  • 2012-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多