windows JavaScript 中的 Window.location.href() 和 Window.open()

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

Window.location.href () and Window.open () in JavaScript

javascriptwindows

提问by khalid jarrah

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

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

回答by Dipak Ingole

window.locationis an Objectand

window.location是一个对象并且

window.location.hrefis its property

window.location.href是它的财产

It tells you the current URL locationof the browser

它告诉你浏览器的当前 URL 位置

document.write(location.href);// will give location URL location of browser.

Setting the property will redirectthe page.

设置该属性将重定向页面。

window.open()is a methodthat you can pass a URL to that you want to open in a new window

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

E.g

例如

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

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

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

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

回答by Lloyd

window.location.hrefchanges the immediate window location.

window.location.href更改直接窗口位置。

window.open()will open a new window.

window.open()将打开一个新的窗口。