Window.location。href和窗口。在JavaScript中打开()方法

时间:2022-10-22 15:07:43

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

window。location有什么不同?href和窗口。在JavaScript中打开()方法?

5 个解决方案

#1


398  

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.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:

windows .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.


Additional Information:

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

window.open()可以传递额外的参数。看:窗口。打开教程

#2


29  

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

    窗口。open将打开一个具有指定URL的新浏览器。

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

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

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.

还要注意,window.open()是窗口对象本身上的函数,而不是窗口。location是一个对象,它公开了各种其他方法和属性。

#3


13  

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

#4


8  

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

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

#5


8  

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

已经有关于windows .location的答案。href属性和window.open()方法。

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.

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

2. Open link in the new or specific window.

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

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

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.

使用window.location.href。得到window.location的价值。href属性。您还可以从窗口获取特定的协议、主机名、hashstring。位置的对象。

See Location Object for more information.

有关更多信息,请参阅Location对象。

#1


398  

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.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:

windows .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.


Additional Information:

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

window.open()可以传递额外的参数。看:窗口。打开教程

#2


29  

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

    窗口。open将打开一个具有指定URL的新浏览器。

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

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

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.

还要注意,window.open()是窗口对象本身上的函数,而不是窗口。location是一个对象,它公开了各种其他方法和属性。

#3


13  

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

#4


8  

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

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

#5


8  

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

已经有关于windows .location的答案。href属性和window.open()方法。

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.

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

2. Open link in the new or specific window.

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

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

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.

使用window.location.href。得到window.location的价值。href属性。您还可以从窗口获取特定的协议、主机名、hashstring。位置的对象。

See Location Object for more information.

有关更多信息,请参阅Location对象。