是否有定义在 document.open 调用中打开的 href 的标准方法?

发布于 2024-12-02 03:02:31 字数 2226 浏览 1 评论 0原文

我正在处理的困境是在 jquery-bbq代码,因为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 打开的页面的默认值

我基本上必须修改代码,以便它不会打开相同的网址两次,因此我希望覆盖默认值并指定一个参数,例如 ?iframe=true。

示例:

http://example.com/views/step-2?one=two

在 IE 中,上面的页面被加载,然后在 iframe 中再次加载。服务器端代码执行两次,但我不希望这种情况发生。

我想将 &iframe=1 附加到 URI,这样如果指定了 iframe 参数,我就可以阻止服务器端代码运行。

两个最明显的事情是:

  1. 设置 iframe 的 src,但这可能会产生负面影响,而且我很确定它的存在是有原因的。
  2. 不要使用 document.open.close,而是使用 iframe.location.href 并将其手动设置为 href。

如果有人有 iframe 黑客攻击的经验,我将非常感谢一些提示。

编辑:我想到的另一种解决方法是通过 iframe/ajax 加载页面,在 iframe 执行 document.open 之前设置会话变量,这会创建一个会话变量'iframeloading' 等于 1,并在 iframe load 上设置一个加载事件处理程序,将其设置回 0,并调整我的服务器端代码,使其在会话变量设置为 1 时不执行。类似于:

$('<iframe src="/iframe-loading.php"/>').hide().appendTo('body');
document.open().close()
 $( document.defaultView ).load(function() {
    $('<iframe src="iframe-doneloading.php/>').hide().appendTo('body');
});

The dilemma that I'm dealing with is that in the jquery-bbq code, because IE6 and IE7 do not support hashchange, they load pages twice in order to have a history state.

This has a negative effect because code runs twice on both server-side and client-side. The method used to create the iframe is this:

  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() );
  }

A blank iframe with a src of javascript:0 is created. The document.open method is called, and it seems like it grabs the location.href of the parent window as the default for the page that opens with .open.

I basically have to modify the code so that it doesn't open the same url twice, so I'm looking to override the default and specify a param such as ?iframe=true.

Example:

http://example.com/views/step-2?one=two

In IE, the page above gets loaded and then gets loaded again in an iframe. The server-side code executes twice, but I don't want that to happen.

I want to append &iframe=1 to the URI so I can prevent my server-side code from running if the iframe param is specified.

The two most obvious things are:

  1. Set the src of the iframe, but this could have a negative effect and I'm pretty sure it's there for a reason.
  2. Instead of use document.open.close, use iframe.location.href and set it to the href manually.

If anyone has had experience with iframe hacking, I'd greatly appreciate some tips.

EDIT: Another workaround I thought of would be loading a page through iframe/ajax that sets a session var before the iframe does the document.open, which creates a session variable of 'iframeloading' which is equal to 1, and set a load event handler on iframe load to set it back to 0, and adjust my server-side code to not execute if the session var is set to 1. Something like:

$('<iframe src="/iframe-loading.php"/>').hide().appendTo('body');
document.open().close()
 $( document.defaultView ).load(function() {
    $('<iframe src="iframe-doneloading.php/>').hide().appendTo('body');
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冷月断魂刀 2024-12-09 03:02:31

关于“是否有定义在 document.open 调用中打开的 href 的标准方法?”

不,没有。

About "Is there a standard way of defining the href that opens in a document.open call?"

No, there is not.

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