JavaScript 中的 window.location.href 和 window.open () 方法

发布于 2024-11-29 16:35:51 字数 93 浏览 1 评论 0原文

JavaScript 中的 window.location.hrefwindow.open () 方法有什么区别?

What is the difference between window.location.href and window.open () methods in JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

情感失落者 2024-12-06 16:35:51

window.location.href 不是一个方法,它是一个属性,可以告诉您浏览器当前的 URL 位置。更改该属性的值将重定向页面。

window.open() 是一种可以将 URL 传递给要在新窗口中打开的方法。例如:

window.location.href 示例:

window.location.href = 'http://www.google.com'; //Will take you to Google.

window.open() 示例:

window.open('http://www.google.com'); //This will open Google in a new window.

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.location.href = 'http://www.google.com'; //Will take you to Google.

window.open() example:

window.open('http://www.google.com'); //This will open Google in a new window.

Additional Information:

window.open() can be passed additional parameters. See: window.open tutorial

穿透光 2024-12-06 16:35:51
  • window.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 whereas window.location is an object that exposes a variety of other methods and properties.

动听の歌 2024-12-06 16:35:51

已经有答案描述了 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.

青柠芒果 2024-12-06 16:35:51

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.

太阳哥哥 2024-12-06 16:35:51

window.open () 将打开一个新窗口,而 window.location.href 将在当前窗口中打开新 URL。

window.open () will open a new window, whereas window.location.href will open the new URL in your current window.

ゞ花落谁相伴 2024-12-06 16:35:51

window.open 将在新浏览器选项卡中打开 url

window.location.href 将在当前选项卡中打开 url(您可以使用 location

这里是示例小提琴(在SO片段中window.open不起作用)

var url = 'https://example.com';

function go1() { window.open(url) }

function go2() { window.location.href = url }

function go3() { location = url }
<div>Go by:</div>
<button onclick="go1()">window.open</button>
<button onclick="go2()">window.location.href</button>
<button onclick="go3()">location</button>

The window.open will open url in new browser Tab

The window.location.href will open url in current Tab (instead you can use location)

Here is example fiddle (in SO snippets window.open doesn't work)

var url = 'https://example.com';

function go1() { window.open(url) }

function go2() { window.location.href = url }

function go3() { location = url }
<div>Go by:</div>
<button onclick="go1()">window.open</button>
<button onclick="go2()">window.location.href</button>
<button onclick="go3()">location</button>

踏月而来 2024-12-06 16:35:51

href 是位置接口的属性。 windos.location.href 导航到提供的 URL。

了解 href

另一方面 windows.open () 将指定资源加载到指定名称下的新的或现有的浏览上下文(即选项卡、窗口或 iframe)中。

open()
open(url)
open(url, target)
open(url, target, windowFeatures)

这些附加参数允许您实现一些特定的功能。有关详细信息,请参阅此文档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.

open()
open(url)
open(url, target)
open(url, target, windowFeatures)

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 use window.open. parent.opener will work if you use location.href for redirection.
Related to issue

海夕 2024-12-06 16:35:51

对于那些使用 React 和 Router 的人来说,这是一种无需刷新整个应用程序即可进行导航的好方法。

withRouter.bind("URL")

调用 window.location.href = "URL" 似乎会刷新应用程序,这可能是一个有意的决定,而且它有效。

For those using React and Router, a nice way to navigate without refreshing the entire app.

withRouter.bind("URL")

Calling window.location.href = "URL" seem to refresh the app and might be an intended decision and it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文