2011-08-16 22 views
1

我试图在两个不同的DIV中加载两个不同的文件。下面是我使用一键点击两个函数

var please_wait = null; 
function open_url(url, target) { 
    if (!document.getElementById) { 
     return false; 
    } 
    if (please_wait != null) { 
     document.getElementById("target").innerHTML = please_wait; 
    } 
    if (window.ActiveXObject) { 
     link = new ActiveXObject("Microsoft.XMLHTTP"); 
    } else if (window.XMLHttpRequest) { 
     link = new XMLHttpRequest(); 
    } 
    if (link == undefined) { 
     return false; 
    } 
    link.onreadystatechange = function() { 
     response(url, target); 
    } 
    link.open("GET", url, true); 
    link.send(null); 
} 
function response(url, target) { 
    if (link.readyState == 4) { 
     document.getElementById(target).innerHTML = (link.status == 200) ? link.responseText : "Ooops!! A broken link! Please contact the webmaster of this website and give him the following errorcode: " + link.status; 
    } 
} 
function set_loading_message(msg) { 
    please_wait = Loding; 
} 

和我使用这个代码的链接调用的代码...

<a href="javascript:void(0)" onclick="open_url('firstpage.php','containerA');open_url('secondpage.php','containerB')"> Click Here </a> 

它只是在加载概情况下,后一个。我尝试了不同的东西,完全采用了两种不同的方法,例如open_url2,但没有用。它一次只能在DIV中加载一个请求。任何解决方案

回答

0

你正在做很多额外的工作,使用旧学校ajax调用。我认为你会用jQuery ajax方法获得更好的运气。

至于为什么后者被加载,你检查了萤火虫或其他网络工具,看看这两个文件是否得到加载? 如果它们都被加载,我相信可能发生的后果是用你正在使用的.innerHTML函数覆盖第一个函数。你想追加。看看这些链接,用jQuery做要容易得多:

Making Ajax calls

Appending to an element

0

你似乎是使用link为全局变量。因此第二次拨打open_url()将覆盖从第一个创建的值link

您应该将其设置为open_url()函数的本地值,并将其传递给response()函数。

var please_wait = null; 
function open_url(url, target) { 

     // v----declare the variable 
    var link; 
    if (!document.getElementById) { 
     return false; 
    } 
    if (please_wait != null) { 
      // ----------------------v---"target" is a String??? 
     document.getElementById("target").innerHTML = please_wait; 
    } 
    if (window.ActiveXObject) { 
     link = new ActiveXObject("Microsoft.XMLHTTP"); 
    } else if (window.XMLHttpRequest) { 
     link = new XMLHttpRequest(); 
    } 
    if (link == undefined) { 
     return false; 
    } 
    link.onreadystatechange = function() { 
     response(url, target, link); // <-- pass "link" to the response 
    } 
    link.open("GET", url, true); 
    link.send(null); 
} 

    // receive "link"-------------v----so that you have the correct xhr object 
function response(url, target, link) { 
    if (link.readyState == 4) { 
     document.getElementById(target).innerHTML = (link.status == 200) ? link.responseText : "Ooops!! A broken link! Please contact the webmaster of this website and give him the following errorcode: " + link.status; 
    } 
} 
function set_loading_message(msg) { 
    please_wait = Loding; 
} 
0

如前所述,您对AJAX有争议。

这里是你的代码如何能期待在jQuery的

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"</script> 

<script type="text/javascript"> 
$(document).ready(function() { 
    $("#link1").click(function() { 
    $("#containerA').load("firstpage.php",function() { 
     $("#containerB').load("secondpage.php"); 
    }); 
    }); 
}); 
</script> 

<a id="link1" href="#"> Click Here </a> 

或者使用一个队列管理器的jQuery插件

+0

感谢很多工作就像一个魅力。 –

相关问题