Javascript window.location.href 和 top.location.href 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3332756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between window.location.href and top.location.href
提问by Egalitarian
Can Anyone tell me the difference between window.location.hrefand top.location.href?
谁能告诉我之间的差异window.location.href和top.location.href?
And also where to use which one.
以及在哪里使用哪个。
And which one will be better when redirecting after an ajax call in mvc?
在 mvc 中进行 ajax 调用后重定向时,哪个会更好?
回答by josh3736
window.location.hrefreturns the location of the current page.
window.location.href返回当前页面的位置。
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, topis a reference to itself (in other words, window=== window.top).
top.location.href(它是 的别名window.top.location.href)返回窗口层次结构中最顶层窗口的位置。如果窗口没有父窗口,top则是对自身的引用(换句话说,window=== window.top)。
topis 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.htmlwith the following script:
top在处理框架和处理由其他页面打开的窗口时都很有用。例如,如果您有一个test.html使用以下脚本调用的页面:
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 – notabout:blank, which is what window.location.hrefwould return.
生成的警报将具有 test.html 的完整路径 -而不是about:blank,这是window.location.href返回的内容。
To answer your question about redirecting, go with window.location.assign(url);
要回答有关重定向的问题,请使用 window.location.assign(url);
回答by Salman A
topobject makes more sense inside frames. Inside a frame, windowrefers to current frame's window while toprefers to the outermost window that contains the frame(s). So:
topobject 在框架内更有意义。在框架内,window指的是当前框架的窗口,而top指的是包含框架的最外面的窗口。所以:
window.location.href = 'somepage.html';means loading somepage.htmlinside the frame.
window.location.href = 'somepage.html';意味着somepage.html在框架内加载。
top.location.href = 'somepage.html';means loading somepage.htmlin the main browser window.
top.location.href = 'somepage.html';表示somepage.html在主浏览器窗口中加载。
回答by meder omuraliev
toprefers to the window object which contains all the current frames ( father of the rest of the windows ). windowis the current window.
top指包含所有当前帧的窗口对象(其余窗口的父亲)。window是当前window.
http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific
http://www.howtocreate.co.uk/tutorials/javascript/browserinspecific
so top.location.hrefcan contain the "master" page link containing all the frames, while window.location.hrefjust contains the "current" page link.
所以top.location.href可以包含包含所有框架的“主”页面链接,而window.location.href只包含“当前”页面链接。
回答by Sachin R
The first one adds an item to your history in that you can (or should be able to) click "Back" and go back to the current page.
第一个向您的历史记录添加一个项目,您可以(或应该能够)单击“返回”并返回当前页面。
The second replaces the current history item so you can't go back to it.
第二个替换当前历史记录项,因此您无法返回它。
See window.location:
见window.location:
assign(url): Load the document at the provided URL.replace(url): Replace the current document with the one at the provided URL. The difference from theassign()method is that after usingreplace()the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
assign(url):在提供的 URL 加载文档。replace(url):用提供的 URL 中的文档替换当前文档。与该assign()方法不同的是,使用replace()当前页面后不会保存在会话历史记录中,这意味着用户将无法使用后退按钮导航到它。
window.location.href = url;
is favoured over:
优先于:
window.location = url;

