user-pic

[随想]也说Ajax

Vote 0 Votes



Ajax现在很热门。 废话我不太会说,简单来个例子
保存为HTML就是了。



<script language="javascript" type="text/javascript">
var req;

function loadXMLDoc(url, parameters)
{
   // branch for native XMLHttpRequest object
   if (window.XMLHttpRequest) {
       //alert("window.XMLHttpRequest");
       req = new XMLHttpRequest();
       req.onreadystatechange = processReqChange;
       req.open("POST", url, true);
       req.setRequestHeader('Connection', 'close');
       req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
       req.send(parameters);
       return;
   }
   // branch for IE/Windows ActiveX version
    if (window.ActiveXObject) {
       //alert("window.ActiveXObject");
       req = new ActiveXObject("Microsoft.XMLHTTP");
       if (req) {
           req.onreadystatechange = processReqChange;
           req.open("POST", url, true);
           req.setRequestHeader('Connection', 'close');
           req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            req.send(parameters);
           return;
       }
   }
   alert("Sorry,Your Browse is so old.");

}

function PreviewMe(input, response)
{
 if (response != ''){
   // Response mode
   message = document.getElementById('ShowitBar');
   message.innerHTML = response;
 }else{
   // Input mode
   url  = 'leobbs.cgi';
   loadXMLDoc(url,'');
 }
}

function processReqChange()
{
  //  only if req shows "complete"
   if (req.readyState == 4) {
       // only if "OK"
       if (req.status == 200) {

     response  = req.responseText;

     PreviewMe('', response);

       } else {
           alert("There was a problem retrieving the XML data:\n" + req.statusText);
       }
   }
}
</script>



<form>
<input type='button' onclick="PreviewMe('','');" value='Preview Me'>
</form>


<div id="ShowitBar" style='background: #FFF; color:#000;padding: 1em'></div>


其中含义自己慢慢领悟:)

PS: 这里有个详细的解释 :

http://www.fayland.org/journal/Ajax_preview.html (非四的PerlSite)

3 Replies

| Add a Reply

Add a Reply

Forum Groups

Good Perl Books

Perl 學習手札

作者:簡信昌

Perl 學習手札 , 一本优秀的中文Perl在线学习资料。

Perl 程序讨论区

12 50

Last Topic: [注意]本区文章归档区 by 路杨 on Oct 16, 2006

事物处理及其他

113 1410

Last Topic: 踩个脚印 by 眼睛oo on Dec 4, 2010

测试私密区

测试私密区

18 88

Notice: 用于测试程序或者不可告人之秘密。不要问我要密码哦。。[此为原LB_ES论坛系统保留项,只限原有权限朋友进入,不再接受注册]

OpenID accepted here Learn more about OpenID