论坛首页 入门技术论坛

AJAX .js

浏览 5604 次
锁定老帖子 主题:AJAX .js
该帖已经被评为新手帖
作者 正文
   发表时间:2008-03-19  
var http_request = false;					//ajax 对象 
function changepage(url,idname)
{
	if(url.indexOf("?")>-1)
		url=url+ "&now="+(new Date()).getTime();
	else
		url=url+ "?now="+(new Date()).getTime();
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = LoadContent(idname); 
		//alert("url="+url)
        http_request.open('GET', url, true);
        http_request.send(null);
}
 

function LoadContent(idname)
{
	
	if (http_request.readyState != 4) 
	{
		//alert("出现错误http_request.readyState"+http_request.readyState);
		return; 
	}
	if (http_request.status != 200)
	{
	 	//alert("出现错误"+http_request.status);;
		return;
	} 
	eval("document.getElementById('"+idname+"').innerHTML") = http_request.responseText;
	//document.getElementById('programlist').innerHTML = "ddddddddddddddddddddddddd";  
}  



比较好用的一个JS
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics