谁能告诉我之间的差异window.location.href和top.location.href?
以及在哪里使用哪个。
在 mvc 中进行 ajax 调用后重定向时,哪个会更好?
谁能告诉我之间的差异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);
第一个向您的历史记录添加一个项目,您可以(或应该能够)单击“返回”并返回当前页面。
第二个替换当前历史记录项,因此您无法返回它。
见window.location:
assign(url):在提供的 URL 加载文档。
replace(url):用提供的 URL 中的文档替换当前文档。与该assign()方法的不同之处在于,使用replace()当前页面后不会保存在会话历史记录中,这意味着用户将无法使用后退按钮导航到该页面。
window.location.href = url;
优于:
window.location = url;
top指包含所有当前帧的窗口对象(其余窗口的父亲)。window是当前window.
http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific
所以top.location.href可以包含包含所有框架的“主”页面链接,而window.location.href只包含“当前”页面链接。