代码之家  ›  专栏  ›  技术社区  ›  Egalitarian

window.location.href和top.location.href之间的差异

  •  82
  • Egalitarian  · 技术社区  · 16 年前

    有谁能告诉我 window.location.href top.location.href ?

    以及在哪里使用哪一个。

    在mvc中ajax调用后重定向哪个更好?

    4 回复  |  直到 14 年前
        1
  •  128
  •   josh3736    16 年前

    window.location.href

    top.location.href (这是 window.top.location.href top 是对自身的引用(换句话说, window window.top ).

    在处理框架和其他页面打开的窗口时都很有用。例如,如果您有一个名为 test.html 使用以下脚本:

    var newWin=window.open('about:blank','test','width=100,height=100');
    newWin.document.write('<script>alert(top.location.href);</script>');
    

    生成的警报将具有test.html的完整路径- about:blank,这是什么 window.location.href

    要回答有关重定向的问题,请使用 window.location.assign(url);

        2
  •  25
  •   Salman Arshad    16 年前

    top 对象在帧内更有意义。在一个框架内, window 引用当前帧的窗口 顶部

    window.location.href = 'somepage.html'; 意味着加载 somepage.html 在框架内。

    top.location.href = 'somepage.html'; 意味着加载 在主浏览器窗口中。

    self parent .

        3
  •  9
  •   meder omuraliev    16 年前

    top 指包含所有当前帧的窗口对象(其余窗口的父窗口)。 window 窗口 .

    http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific

    所以 top.location.href 可以包含包含所有框架的“母版”页链接,而 window.location.href 只包含“当前”页面链接。

        4
  •  7
  •   Nisse Engström sting_roc    9 年前

    第一种方法是在历史记录中添加一个条目,您可以(或者应该能够)单击“后退”并返回到当前页面。

    看到了吗 window.location :

    • assign(url)

    • replace(url) :将当前文档替换为所提供URL处的文档。不同于 assign() 方法是使用后 replace()

    window.location.href = url;
    

    优先于:

    window.location = url;