一.静态页代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/javascript" language="javascript">
//创建适用用与多种浏览器的XMLHttpRequest对象的函数
var XMLHttpReq = false;
//创建 XMLHttpRequest 对象(主函数不需改变)
function createXMLHttpRequest(){
if(window.XMLHttpRequest){ // Mozilla 浏览器
XMLHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject){ // IE 浏览器
try{
XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{
XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
}
//发送请求函数(提交xml格式的请求参数)
function sendRequest(url){
createXMLHttpRequest();
XMLHttpReq.open("POST",url,false);
XMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XMLHttpReq.send();//发送请求
}
sendRequest("CheckLogin.aspx"); //这里是只有后台代码的“检测是否登录页”
var i= XMLHttpReq.responseText;
i=i.substring(0,1);
if(i=="0")
location.href="Login.aspx"; //这是“登录页”
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>静态页</title>
</head>
<body>
这是静态页
</body>
</html>
二.新建一个aspx页面,用来检测是否登录,该页面没有显示,只有后台代码如下:
public partial class CheckLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str="www";
HttpCookie cookie=new HttpCookie("user",str);
Response.Cookies.Add(cookie);
if (Request.Cookies["user"] != null)
{
Response.Write("1"); //这里输出的值即静态页获得的XMLHttpReq.responseText
}
else
Response.Write("0");
}
}