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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7077770/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:44:59  来源:igfitidea点击:

window.location.href and window.open () methods in JavaScript

javascriptlocationwindowhrefwindow-object

提问by masif

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

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

回答by James Hill

window.location.hrefis nota 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.location.href不是一种方法,它是一个属性,它会告诉你浏览器的当前URL位置。更改属性的值将重定向页面。

window.open()is a method that you can pass a URL to that you want to open in a new window. For example:

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

window.location.href example:

window.location.href 示例:

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

window.open() example:

window.open() 示例:

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



附加信息:

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

window.open()可以传递额外的参数。参见:window.open 教程

回答by Tom

  • window.openwill open a new browser with the specified URL.

  • window.location.hrefwill open the URL in the window in which the code is called.

  • window.open将打开一个具有指定 URL 的新浏览器。

  • window.location.href将在调用代码的窗口中打开 URL。

Note also that window.open()is a function on the window object itself whereas window.locationis an object that exposes a variety of other methods and properties.

另请注意,这window.open()是 window 对象本身上的一个函数,而window.location是一个公开各种其他方法和属性的对象

回答by ngi

window.openis 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 只是当前窗口的一个属性。

回答by Somnath Muluk

There are already answers which describes about window.location.hrefproperty and window.open()method.

已经有描述window.location.href属性和window.open()方法的答案。

I will go by Objective use:

我将通过目标使用:

1. To redirect the page to another

1.将页面重定向到另一个

Use window.location.href. Set href property to the href of another page.

使用 window.location.href。将 href 属性设置为另一个页面的 href。

2. Open link in the new or specific window.

2. 在新窗口或特定窗口中打开链接。

Use window.open(). Pass parameters as per your goal.

使用 window.open()。根据您的目标传递参数。

3. Know current address of the page

3. 知道页面的当前地址

Use window.location.href. Get value of window.location.href property. You can also get specific protocol, hostname, hashstring from window.location object.

使用 window.location.href。获取 window.location.href 属性的值。您还可以从 window.location 对象中获取特定的协议、主机名、哈希字符串。

See Location Objectfor more information.

有关更多信息,请参阅位置对象

回答by Joseph Silber

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

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

回答by Kamil Kie?czewski

The window.openwill open url in new browser Tab

window.open会在新的浏览器标签页中打开链接

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

window.location.href会在当前标签页中打开链接(而不是你可以使用location

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

这是示例小提琴(在 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>