window.location.href和top.location.href之间的区别
任何人都可以告诉我window.location.href和top.location.href之间的区别?
还有哪里可以使用哪一个。
在ajax调用mvc之后redirect哪一个会更好?
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的完整path – 而不是 about:blank,这是window.location.href将返回的内容。
要回答你关于redirect的问题,请使用window.location.assign(url);
top对象在帧内更有意义。 在一个框架内, window是指当前框架的窗口,而top是指包含框架的最外面的窗口。 所以:
window.location.href = 'somepage.html'; 意味着在框架内加载somepage.html 。
top.location.href = 'somepage.html'; 意味着在主浏览器窗口中加载somepage.html 。
另外两个有趣的对象是self和parent 。
top指的是包含所有当前帧的窗口对象(其余窗口的父亲)。 window是当前window 。
http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific
所以top.location.href可以包含包含所有框架的“主”页面链接,而window.location.href只包含“当前”页面链接。
第一个向您的历史添加一个项目,你可以(或应该能够)点击“返回”并返回到当前页面。
第二个replace当前的历史项目,所以你不能回到它。
请参阅window.location :
-
assign(url):在提供的URL中加载文档。 -
replace(url):用提供的URLreplace当前文档。 与assign()方法的不同之处在于,在使用replace(),当前页面将不会保存在会话历史logging中,这意味着用户将无法使用“后退”button导航到该页面。
window.location.href = url;
比以往更受青睐:
window.location = url;