Javascript 一键实现两个功能

发布于 2024-11-29 12:06:46 字数 1312 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2024-12-06 12:06:46

通过使用老式的 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

甜宝宝 2024-12-06 12:06:46

您似乎正在使用 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;
}

You appear to be using link as a global variable. Therefore the second call to open_url() is overwriting the value of link created from the first.

You should make it local to the open_url() function, and pass it to the response() function.

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;
}
川水往事 2024-12-06 12:06:46

正如已经说过的,您在 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 的队列管理器插件

As already said, you have contention on the AJAX.

Here is how your code could look in 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>

Alternatively use a queue manager plugin for jQuery

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文