<script type="text/javascript">
function makeRequest() {
var guid = rnd();
//記住給個(gè)隨機(jī)數(shù),不然的話有緩存
var url = "example_b.aspx?id=" + guid;
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleRefresh;
xmlHttp.open("GET", url, false);
xmlHttp.send(null);
}
//創(chuàng)建xmlHttp
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
//返回信息
function handleRefresh() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
if (xmlHttp.responseText == "sessionNull") {
//alert('登錄超時(shí),帳戶注銷,請(qǐng)重新登錄...');
window.parent.document.location = "UserLogin.aspx";
}
setTimeout("makeRequest()", 8*000);
}
}
}
//創(chuàng)建隨機(jī)數(shù)
rnd.today = new Date();
rnd.seed = rnd.today.getTime();
function rnd() {
rnd.seed = (rnd.seed * 9301 + 49297) % 233280;
return rnd.seed / (233280.0);
}
function rand(number) {
return Math.ceil(rnd() * number);
}
</script>
舉個(gè)例子,比如說(shuō)要example_a.aspx頁(yè)面加載以后讓測(cè)試頁(yè)面example_b.aspx每8秒刷新一次,檢查存放登錄用戶名的session值是否為空,如果session為空,則返回登錄頁(yè)面。那么我們可以將上面的js代碼放在example_a.aspx頁(yè)面的<head></head>標(biāo)記之間,然后在頁(yè)面body的onload事件中調(diào)用上面的方法:onload="makeRequest()",然后在頁(yè)面的Page_Load事件中判斷:
if (Session["user"] == null)
Code
{
Response.Clear();
Response.Write("sessionNull");
Response.End();
}