用Ajax实现静态页面中的判断

动态加载网页内容
本文介绍了一种使用JavaScript结合ASP及XMLHttpRequest实现网页内容动态加载的方法。通过定义setInnerHTML和getInnerHTML函数来设置和获取HTML元素的内容,并利用XMLHttpRequest对象从服务器请求数据。服务器端脚本showcontent.asp响应请求并返回特定内容。

1、静态文件123.html

 

<html>

<script type="text/javascript" src="showcontent.js"></script>

<body onload="ShowContent()">

<div id="content"></div>

</body>

</html>

 

2、JS文件showcontent.js

 

// JavaScript Document for set innerHTML
function setInnerHTML(objId, html){
 
try{
  var obj 
= document.getElementById(objId);
  obj.innerHTML 
= html;
 }
catch(exception){
  alert(
"Make sure the object id is correct!");
 }
}

function getInnerHTML(objId){
 
try{
  var obj 
= document.getElementById(objId);
  
return obj.innerHTML;
 }
catch(exception){
  alert(
"Make sure the object id is correct!");
 } 
}
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp 
= false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
   xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
   try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (e2) {
      xmlHttp = false;
   }
}
@end @
*/

if (!xmlHttp && typeof XMLHttpRequest != "undefined") {
  xmlHttp 
= new XMLHttpRequest();
}


function ShowContent(){
 
try{
  var url 
= "showcontent.asp";
  xmlHttp.open(
"GET", url, true);
  xmlHttp.onreadystatechange 
= ContentOk;
  xmlHttp.send(
null); 
 }
catch(exception){} 
}

function ContentOk(){
 
if (xmlHttp.readyState == 4) {
  
try{
   var response 
= xmlHttp.responseText;
   var alertObj 
= document.getElementById("Content");
   alertObj.innerHTML
=response;
  }
catch(exception){}
 } 
}

 

3、脚本文件showcontent.asp

 

<%

Response.Expires 
= -1
Response.ExpiresAbsolute 
= Now() - 1
Response.cachecontrol 
= "no-cache"

if session("id")="" then

str
="请先登录"

else

str
="123456"

end if

Response.AddHeader 
"Content-Type","text/html; charset=gb2312"

Response.Write 
""&str&""

%
>

 

 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值