AJAX的小例子

本文介绍了一个简单的Ajax应用实例,通过JavaScript实现网页的部分内容动态加载而无需刷新整个页面。当点击特定链接时,将调用服务器端脚本并获取响应数据,然后更新对应div元素的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件:div.html

<html>
<body>
<script>

function InitAjax()
{
 var ajax=false;
 try {
  ajax = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   ajax = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   ajax = false;
  }
 }
 if (!ajax && typeof XMLHttpRequest!='undefined') {
  ajax = new XMLHttpRequest();
 }
 return ajax;
}

 

function show(prov_code)
{
 var div_name = "div_" + prov_code;
 var div_entity = document.getElementById(div_name);
 if ("" == div_entity.innerHTML)
 {
  var ajax = InitAjax();
  var url = "cgi-bin/test.pl?prov=" + prov_code;
  ajax.open("GET", url, true);
  ajax.onreadystatechange = function() {
    if (ajax.readyState == 4 && ajax.status == 200)
   {
    div_entity.innerHTML = ajax.responseText;
    div_entity.style.display = "block";
       }
  }
  ajax.send(null);
 }
 else
 {
  div_entity.innerHTML = "";
  div_entity.style.display = "none";
 }
}

function hidden()
{
 document.getElementById("div").style.display="none";
}
</script>
<BODY>
<H2 align="center"><a name="100" href="#" onclick="show(100);">100</a></H2>
<div id="div_100"></div>
<H2 align="center"><a name="210" href="#" onclick="show(210);">210</a></H2>
<div id="div_210"></div>
</BODY>
</HTML>
 ------------------------------------

文件test.pl

#!c:/perl/bin/perl.exe
##
##  printenv -- demo CGI program which just prints its environment
##

#print "Content-type: text/html/n/n";
print "Content-type: text/plain/n/n";

open(FILE_TXT,'C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/input_txt/1.txt') or print 'abc';

while (<FILE_TXT>)
{
 print;
}

print "$ENV{QUERY_STRING}";
#print 'CGI---bbb!';
#print '<HTML><BODY><h1>CGI执行成功!</h1></BODY></HTML>';
#print '<br><input type=text id="163id">';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值