【发布时间】:2011-11-06 05:05:36
【问题描述】:
我正在处理的困境是在jquery-bbq code 中,因为IE6 和IE7 不支持hashchange,它们会加载页面两次以获得历史状态。
这会产生负面影响,因为代码会在服务器端和客户端运行两次。用于创建 iframe 的方法是这样的:
if ( is_old_ie ) {
// Create hidden IFRAME at the end of the body.
iframe = $('<iframe src="javascript:0"/>').hide().appendTo( 'body' )[0].contentWindow;
// Get history by looking at the hidden IFRAME's location.hash.
get_history = function() {
return get_fragment( iframe.document.location.href );
};
// Set a new history item by opening and then closing the IFRAME
// document, *then* setting its location.hash.
set_history = function( hash, history_hash ) {
if ( hash !== history_hash ) {
var doc = iframe.document;
doc.open().close();
doc.location.hash = '#' + hash;
}
};
// Set initial history.
set_history( get_fragment() );
}
创建了一个 src 为 javascript:0 的空白 iframe。调用了document.open 方法,它似乎将父窗口的location.href 作为以.open 打开的页面的默认值。
我基本上必须修改代码,使其不会两次打开相同的 url,所以我希望覆盖默认值并指定一个参数,例如 ?iframe=true。
例子:
http://example.com/views/step-2?one=two
在 IE 中,上面的页面被加载,然后在 iframe 中再次加载。服务器端代码执行两次,但我不希望发生这种情况。
我想将 &iframe=1 附加到 URI,这样如果指定了 iframe 参数,我就可以阻止我的服务器端代码运行。
最明显的两件事是:
- 设置 iframe 的 src,但这可能会产生负面影响,我很确定它的存在是有原因的。
- 不要使用
document.open.close,而是使用iframe.location.href并手动将其设置为href。
如果有人有过 iframe 黑客攻击的经验,我将不胜感激。
编辑:我想到的另一个解决方法是通过 iframe/ajax 加载页面,在 iframe 执行 document.open 之前设置会话变量,这会创建一个会话变量“iframeloading”等于 1,并在 iframe 加载时设置加载事件处理程序以将其设置回 0,并调整我的服务器端代码以在会话 var 设置为 1 时不执行。类似于:
$('<iframe src="/iframe-loading.php"/>').hide().appendTo('body');
document.open().close()
$( document.defaultView ).load(function() {
$('<iframe src="iframe-doneloading.php/>').hide().appendTo('body');
});
【问题讨论】:
-
如果我给你一个用 jquery 处理 hashchange 事件的替代方法,它会解决你的问题吗?
-
如果你能解决 hashchange 加载两次的问题,它会为你节省很多时间。 jQuery BBQ 文档提到:“确保绑定到 document.ready 上的“hashchange”事件,而不是之前,否则它可能在 IE6/7 中失败。那不是你的问题,是吗?此外,您可以将您的问题发布到项目 GitHub 页面github.com/cowboy/jquery-bbq/issues
标签: jquery internet-explorer-7 internet-explorer-6 jquery-bbq