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
window.location.href and window.open () methods in JavaScript
提问by masif
What is the difference between window.location.href
and window.open ()
methods in JavaScript?
JavaScript 中的window.location.href
和window.open ()
方法有什么区别?
回答by James Hill
window.location.href
is 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.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.
window.open
将打开一个具有指定 URL 的新浏览器。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()
是 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.href
will open the new URL in your current window.
window.open ()
将打开一个新窗口,而window.location.href
将在当前窗口中打开新 URL。
回答by Kamil Kie?czewski
The window.open
will open url in new browser Tab
该window.open
会在新的浏览器标签页中打开链接
The window.location.href
will 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>