如何使用 AJAX 替代 iframe
我正在尝试构建一个快速的 Web 应用程序,在 GUI 加载后利用 JS、Prototype 和 AJAX 来满足我的所有请求。该应用程序很简单:一组链接和一个容器元素,用于显示链接指向的任何内容,就像 iframe 一样。下面是一个大概的 HTML 片段:
<a class="ajax" href="/somearticle.html">An article</a>
<a class="ajax" href="/anotherarticle.html">Another article</a>
<a class="ajax" href="/someform.html">Some form</a>
<div id="ajax-container"></div>
上面附带的 JS(抱歉有点长)看起来像这样:
document.observe('dom:loaded', function(event) {
ajaxifyLinks(document.documentElement);
ajaxifyForms(document.documentElement);
});
function ajaxifyLinks(container) {
container.select('a.ajax').each(function(link) {
link.observe('click', function(event) {
event.stop();
new Ajax.Updater($('ajax-container'), link.href, {
method: 'get',
onSuccess: function(transport) {
// Make sure new ajax-able elements are ajaxified
ajaxifyLinks(container);
ajaxifyForms(container);
}
});
});
});
}
function ajaxifyForms(container) {
console.debug('Notice me');
container.select('form.ajax').each(function(form) {
form.observe('submit', function(event) {
event.stop();
form.request({
onSuccess: function(transport) {
$('ajax-container').update(transport.responseText);
// Make sure new ajax-able elements are ajaxified
ajaxifyLinks(container);
ajaxifyForms(container);
}
});
});
});
}
单击链接时,响应将显示在容器中。我在这里没有使用 iframe 作为容器,因为我希望页面上的任何元素都能够在某个时刻通过 JS 相互通信。现在,有一个大问题和一个奇怪的现象:
问题:如果返回一个表单并在容器中显示,上面的 JS 会尝试对表单应用相同的行为,以便提交后收到的任何响应都会显示在容器中容器。这会失败,因为提交事件永远不会被捕获。为什么?请注意,所有返回的表单元素都具有 class="ajax"
属性。
现象:注意 ajaxifyForms()
中的 console.debug()
语句。我希望它在页面加载后输出到控制台一次,然后每次使用表单更新容器时输出到控制台。事实上,每次单击指向表单的链接时,控制台的输出数量似乎都会增加一倍。为什么?
I'm trying to put together a snappy webapp, utilizing JS, Prototype and AJAX for all my requests once the GUI has loaded. The app is simple: A set of links and a container element to display whatever the links point to, just like an iframe. Here's an approximate HTML snippet:
<a class="ajax" href="/somearticle.html">An article</a>
<a class="ajax" href="/anotherarticle.html">Another article</a>
<a class="ajax" href="/someform.html">Some form</a>
<div id="ajax-container"></div>
The JS that accompanies the above (sorry it's a bit lengthy) looks like this:
document.observe('dom:loaded', function(event) {
ajaxifyLinks(document.documentElement);
ajaxifyForms(document.documentElement);
});
function ajaxifyLinks(container) {
container.select('a.ajax').each(function(link) {
link.observe('click', function(event) {
event.stop();
new Ajax.Updater($('ajax-container'), link.href, {
method: 'get',
onSuccess: function(transport) {
// Make sure new ajax-able elements are ajaxified
ajaxifyLinks(container);
ajaxifyForms(container);
}
});
});
});
}
function ajaxifyForms(container) {
console.debug('Notice me');
container.select('form.ajax').each(function(form) {
form.observe('submit', function(event) {
event.stop();
form.request({
onSuccess: function(transport) {
$('ajax-container').update(transport.responseText);
// Make sure new ajax-able elements are ajaxified
ajaxifyLinks(container);
ajaxifyForms(container);
}
});
});
});
}
When clicking a link, the response is displayed in the container. I'm not using an iframe for the container here, because I want whatever elements are on the page to be able to communicate with each other through JS at some point. Now, there is one big problem and one curious phenomenon:
Problem: If a form is returned and displayed in the container, the JS above tries to apply the same behavior to the form, so that whatever response is received after submitting is displayed in the container. This fails, as the submit event is never caught. Why? Note that all returned form elements have the class="ajax"
attribute.
Phenomenon: Notice the console.debug()
statement in ajaxifyForms()
. I expect it to output to the console once after page load and then every time the container is updated with a form. The truth is that the number of outputs to the console seems to double for each time you click a link pointing to a form. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了另一种方法来实现我想要的。事实上,这样做的代码更小并且更不容易出错。我没有尝试确保在任何给定时间都观察到页面上的每个链接和表单元素,而是利用事件冒泡并仅侦听文档本身。检查冒泡到它的每个事件,我可以确定它是否是 AJAX 请求的对象。这是新的 JS:
工作起来就像一个魅力:)
I found another way to achieve what I wanted. In fact, the code for doing so is smaller and is less error prone. Instead of trying to make sure each link and form element on the page is observed at any given time, I utilize event bubbling and listen only to the document itself. Examining each event that bubbles up to it, I can determine whether it is subject for an AJAX request or not. Here's the new JS:
Works like a charm :)