JavaScript 中的 window.location.href 和 window.open () 方法
JavaScript 中的 window.location.href
和 window.open ()
方法有什么区别?
What is the difference between window.location.href
and window.open ()
methods in JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
window.location.href
不是一个方法,它是一个属性,可以告诉您浏览器当前的 URL 位置。更改该属性的值将重定向页面。window.open()
是一种可以将 URL 传递给要在新窗口中打开的方法。例如:window.location.href 示例:
window.open() 示例:
Additional Information:
window.open()
可以传递附加参数。请参阅:window.open 教程window.location.href
is not a method, it's a property that will tell you the current URL location of the browser. Changing the value of the property will redirect the page.window.open()
is a method that you can pass a URL to that you want to open in a new window. For example:window.location.href example:
window.open() example:
Additional Information:
window.open()
can be passed additional parameters. See: window.open tutorialwindow.open
将使用指定的 URL 打开新的浏览器。window.location.href
将在调用代码的窗口中打开 URL。另请注意,
window.open()
是窗口对象本身的函数,而window.location
是公开各种 其他方法和属性。window.open
will open a new browser with the specified URL.window.location.href
will open the URL in the window in which the code is called.Note also that
window.open()
is a function on the window object itself whereaswindow.location
is an object that exposes a variety of other methods and properties.已经有答案描述了 window.location.href 属性和 window.open() 方法。
我将按照目标使用:
1. 将页面重定向到另一个
使用 window.location.href。将 href 属性设置为另一个页面的 href。
2. 在新窗口或特定窗口中打开链接。
使用window.open()。根据您的目标传递参数。
3. 知道页面的当前地址
使用window.location.href。获取 window.location.href 属性的值。您还可以从 window.location 对象获取特定的协议、主机名、哈希字符串。
有关详细信息,请参阅位置对象。
There are already answers which describes about window.location.href property and window.open() method.
I will go by Objective use:
1. To redirect the page to another
Use window.location.href. Set href property to the href of another page.
2. Open link in the new or specific window.
Use window.open(). Pass parameters as per your goal.
3. Know current address of the page
Use window.location.href. Get value of window.location.href property. You can also get specific protocol, hostname, hashstring from window.location object.
See Location Object for more information.
window.open 是一种方法;您可以打开新窗口,并可以自定义它。
window.location.href 只是当前窗口的一个属性。
window.open is a method; you can open new window, and can customize it.
window.location.href is just a property of the current window.
window.open ()
将打开一个新窗口,而window.location.href
将在当前窗口中打开新 URL。window.open ()
will open a new window, whereaswindow.location.href
will open the new URL in your current window.window.open
将在新浏览器选项卡中打开 urlwindow.location.href
将在当前选项卡中打开 url(您可以使用location
)这里是示例小提琴(在SO片段中window.open不起作用)
The
window.open
will open url in new browser TabThe
window.location.href
will open url in current Tab (instead you can uselocation
)Here is example fiddle (in SO snippets window.open doesn't work)
href
是位置接口的属性。windos.location.href
导航到提供的 URL。了解 href
另一方面 windows.open () 将指定资源加载到指定名称下的新的或现有的浏览上下文(即选项卡、窗口或 iframe)中。
这些附加参数允许您实现一些特定的功能。有关详细信息,请参阅此文档window.open 文档
在本文档之外,我注意到的一件有趣的事情是,如果您使用
window.open
,则在 safari 和 IOS 中,parent.opener
是未定义的。如果您使用 location.href 进行重定向,则parent.opener
将起作用。与问题相关
href
is a property of the location interface.windos.location.href
navigates to the provided URL.Read about href
On the other hand windows.open() loads a specified resource into a new or existing browsing context(that is, a tab, a window, or an iframe) under a specified name.
These additional parameters allow you to implement some specific functionalities. Refer to this document for details window.open documentation
One interesting thing that I noticed beyond this document is that
parent.opener
is undefined in case of safari and IOS if you usewindow.open
.parent.opener
will work if you use location.href for redirection.Related to issue
对于那些使用 React 和 Router 的人来说,这是一种无需刷新整个应用程序即可进行导航的好方法。
调用
window.location.href = "URL"
似乎会刷新应用程序,这可能是一个有意的决定,而且它有效。For those using React and Router, a nice way to navigate without refreshing the entire app.
Calling
window.location.href = "URL"
seem to refresh the app and might be an intended decision and it works.