Javascript 一键实现两个功能
我正在尝试在 2 个不同的 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 中一次仅加载一个请求。任何解决方案。
I'm trying to load two different files in 2 different DIVs. Here is the code I'm using
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;
}
and for call on a link I'm using this code...
<a href="javascript:void(0)" onclick="open_url('firstpage.php','containerA');open_url('secondpage.php','containerB')"> Click Here </a>
It simply loads the later one in WHATSOEVER case. I've tried a hell different things, made absolutely two different methods, e.g open_url2 but no avail. It loads only one request at one time in a DIV. Any solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过使用老式的 ajax 调用,您需要做很多额外的工作。我认为使用 jQuery ajax 方法你会运气更好。
至于为什么后者会被加载,您是否检查过firebug或其他网络工具以查看这两个文件是否都已加载? 如果它们都被加载,我相信可能发生的情况是后者用您正在使用的 .innerHTML 函数覆盖第一个。你想追加。查看这些链接,使用 jQuery 会更容易:
进行 Ajax 调用
附加到元素
You're doing a lot of extra work by using old school ajax calls. I think you'd have better luck with jQuery ajax methods.
As for why the latter gets loaded, have you checked firebug or other network tools to see if both files do get loaded? If they are both getting loaded, I believe that what's probably happening is the latter is overwriting the first with that .innerHTML function you're using. You want to append. Check out these links, doing it with jQuery would be much easier:
Making Ajax calls
Appending to an element
您似乎正在使用
link
作为全局变量。因此,对open_url()
的第二次调用将覆盖第一次创建的link
的值。您应该将其设置为
open_url()
函数的本地函数,并将其传递给response()
函数。You appear to be using
link
as a global variable. Therefore the second call toopen_url()
is overwriting the value oflink
created from the first.You should make it local to the
open_url()
function, and pass it to theresponse()
function.正如已经说过的,您在 AJAX 上存在争用。
以下是您的代码在 jQuery 中的外观
或者使用 jQuery 的队列管理器插件
As already said, you have contention on the AJAX.
Here is how your code could look in jQuery
Alternatively use a queue manager plugin for jQuery