2011-06-28 133 views
0

我正在为邮件列表注册并尝试使页面和js文件不被浏览器缓存。因此,我使用假querystring。我试图让页面刷新时创建一个新的查询字符串。它不工作...为什么?我认为该行:javascript事件永远不会触发

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")); 
} 
+0

我曾经让它触发无尽,但现在它根本没有触发。 – diracdeltafunk

+0

当它无休止地触发我有一个不好的地方的代码。现在可能是这样。 – diracdeltafunk

+0

我在javascript中看不到方法makePopUp,它是否在另一个文件中,添加alert('something');之前makePopUp在onclick事件 –

回答

0

的广告给你的头部

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

该指令CACHE-CONTROL:NO-CACHE表明缓存的信息不应该被使用,取而代之的请求应该被转发到原始服务器。

+0

@SenadMeškin我知道这是一个选项,但我不想使用它的原因很多。 – diracdeltafunk

+0

和其中之一? –

+0

@SenadMeškin我希望能够让我的JavaScript不缓存。 – diracdeltafunk

相关问题