使用 JavaScript 实现 XMLHttpRequest,在IE,FireFox 上测试通过!

原创 2005年05月25日 15:37:00

XMLHttp 方式实现无刷屏,在IE,FireFox 上测试通过

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style>
html {background-color:#eeeeee}
body {
      background-color:#ccffcc;
      font-family:Tahoma,Arial,Helvetica,sans-serif;
      font-size:12px;
   margin-left:15%;
   margin-right:15%;
   border:3px groove #006600;
   padding:15px
  }
h1   {
      text-align:left;
      font-size:1.5em;
      font-weight:bold
     }
</style>
<script type="text/javascript">
// global flag
var isIE = false;

// global request and XML document objects
var req;

// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            clearTopicList();
            buildTopicList();
         } else {
            alert("There was a problem retrieving the XML data:/n" +
                req.statusText);
         }
    }
}

// invoked by "Category" select element change;
// loads chosen XML document, clears Topics select
// element, loads new items into Topics select element
function loadDoc(evt) {
    // equalize W3C/IE event models to get event object
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    if (evt) {
        // equalize W3C/IE models to get event target reference
        var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if (elem) {
            try {
                if (elem.selectedIndex > 0) {
                    loadXMLDoc(elem.options[elem.selectedIndex].value);
                } 
            }
            catch(e) {
                var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
                alert("Unable to get XML data:/n" + msg);
                return;
            }
        }
    }
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;      
        }
    } else {
        return "n/a";
    }
}

// empty Topics select list content
function clearTopicList() {
    var select = document.getElementById("topics");
    while (select.length > 0) {
        select.remove(0);
    }
}

// add item to select element the less
// elegant, but compatible way.
function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}

// fill Topics select list with items from
// the current XML document
function buildTopicList() {
    var select = document.getElementById("topics");
    var items = req.responseXML.getElementsByTagName("item");
    // loop through <item> elements, and add each nested
    // <title> element to Topics select element
    for (var i = 0; i < items.length; i++) {
        appendToSelect(select, i,
            document.createTextNode(getElementTextNS("", "title", items[i], 0)));
    }
    // clear detail display
    document.getElementById("details").innerHTML = "";
}

// display details retrieved from XML document
function showDetail(evt) {
    evt = (evt) ? evt : ((window.event) ? window.event : null);
    var item, content, div;
    if (evt) {
        var select = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if (select && select.options.length > 1) {
            // copy <content:encoded> element text for
            // the selected item
            item = req.responseXML.getElementsByTagName("item")[select.value];
            content = getElementTextNS("content", "encoded", item, 0);
            div = document.getElementById("details");
            div.innerHTML = "";
            // blast new HTML content into "details" <div>
            div.innerHTML = content;
        }
    }
}
</script>
</head>
<body>
<h1>XMLHttpRequest Object Demo</h1>
<hr />

<form>
<p>Category:<br />
<select onchange="loadDoc(event)">
    <option value="">Choose One</option>
    <option value="songs.xml">Top 10 Songs</option>
    <option value="albums.xml">Top 10 Albums</option>
    <option value="newreleases.xml">Top 10 New Releases</option>
    <option value="justadded.xml">Top 10 Just Added</option>
</select>
</p>
<p>Items:<br />
<select size="10" id="topics" onchange="showDetail(event)">
    <option value="">Choose a Category First</option>
</select>
</p>
</form>
<div id="details"><span></span></div>
</body>
<html>

利用JQuery实现网页左侧树形菜单(IE6 IE7 Firefox测试通过)

本次实验项目结构图:   1. 定义保存菜单数据的表结构 CREATE TABLE `tb_com_menu_info` ( `MENU_ID` varchar(8) NOT NUL...

jquery操作iframe(齐全版,经IE6&IE6+、firefox、chrome测试通过)

父窗口获取子窗口元素  子窗口获取父窗口元素 子窗口获取子窗口元素 附注:出于安全考虑,浏览器不允许脚本跨域访问iframe,谷歌浏览器离线状态下认为脚本访问iframe为跨域访问(跟cookie类似...

javascript跨Iframe遮罩层.(IE6 IE7 IE8 FF测试通过)

近日,被遮罩层折腾得快要吐血了.而且最近用jquery也发现了诸多问题.所以就自己用javascript写了一个遮罩层.与大家分享一下,小弟初学,不足之处望请各位高手勿拍,多多指教, 由于时间有限,...
  • vipyhd
  • vipyhd
  • 2012年06月06日 08:25
  • 423

XMLHttpRequest 乱码问题 在firefox IE的不同解决方法

XMLHttpRequest乱码问题在Firefox与IE下的不同解决方法 XMLHttpRequest 默认是用UTF-8 传递数据。当服务端的返回数据是UTF-8编码的时候,它工作得很好...

javascript+css 实现右键菜单 (IE, firefox,chrome)

 基本原理:我们知道鼠标的右键单击事件是通过document.oncontextmenu来调用的,如果我们自行定义document.oncontextmenu=某个函数,这样就可以实现新右键菜单的调用...

兼容最新firefox、chrome和IE的javascript图片预览实现代码

转自:http://www.jb51.net/article/53435.htm 这篇文章主要介绍了兼容最新firefox、chrome和IE的javascript图片预览实现代码,...
  • klx502
  • klx502
  • 2016年06月02日 20:30
  • 381

Html5 Canvas 扫雷 (IE9测试通过)

====================================================== 注:本文源代码点此下载 =============================...

Firefox3中使用XMLHttpRequest(Ajax)发送二进制POST数据的简易调试法

XHR(XMLHttpRequest)发送POST文本数据的情况还算常见,直接调用“xhr.send(data)”即可。但是对于二进制数据的处理稍微复杂。 不过在Firefox3中,可以借助下面的方法...

firefox与ie 的javascript区别

  • 2012年03月05日 11:53
  • 251KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:使用 JavaScript 实现 XMLHttpRequest,在IE,FireFox 上测试通过!
举报原因:
原因补充:

(最多只允许输入30个字)