window.location.href 和 top.location.href 的区别 [英] Difference between window.location.href and top.location.href
问题描述
谁能告诉我window.location.href
和top.location.href
的区别?
以及在哪里使用哪个.
在 mvc 中进行 ajax 调用后重定向时,哪个会更好?
window.location.href
返回当前页面的位置.
top.location.href
(window.top.location.href
的别名)返回窗口层次结构中最顶层窗口的位置.如果一个窗口没有父窗口,top
是对自身的引用(换句话说,window
=== window.top
).>
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);
Can Anyone tell me the difference between window.location.href
and top.location.href
?
And also where to use which one.
And which one will be better when redirecting after an ajax call in mvc?
window.location.href
returns the location of the current page.
top.location.href
(which is an alias of window.top.location.href
) returns the location of the topmost window in the window hierarchy. If a window has no parent, top
is a reference to itself (in other words, window
=== window.top
).
top
is useful both when you're dealing with frames and when dealing with windows which have been opened by other pages. For example, if you have a page called test.html
with the following script:
var newWin=window.open('about:blank','test','width=100,height=100');
newWin.document.write('<script>alert(top.location.href);</script>');
The resulting alert will have the full path to test.html – not about:blank, which is what window.location.href
would return.
To answer your question about redirecting, go with window.location.assign(url);
这篇关于window.location.href 和 top.location.href 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!