Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的window.location.href和window.open()方法_Javascript_Location_Window_Href_Window Object - Fatal编程技术网

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

JavaScript中的window.location.href和window.open()方法,javascript,location,window,href,window-object,Javascript,Location,Window,Href,Window Object,JavaScript中的window.location.href和window.open()方法有什么区别?window.open()将打开一个新窗口,而window.location.href将在当前窗口中打开新的URL。window.location.href不是方法,该属性将告诉您浏览器的当前URL位置。更改属性值将重定向页面 window.open()是一种方法,您可以将URL传递给要在新窗口中打开的URL。例如: window.location.href示例: window.locat

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

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

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

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

window.location.href示例:

window.location.href = 'http://www.google.com'; //Will take you to Google.
window.open('http://www.google.com'); //This will open Google in a new window.
window.open()示例:

window.location.href = 'http://www.google.com'; //Will take you to Google.
window.open('http://www.google.com'); //This will open Google in a new window.

其他信息:
window.open()
可以传递其他参数。请参阅:

  • 窗口。打开
    将使用指定的URL打开新浏览器


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

还要注意的是,
window.open()
是window对象本身的函数,而
window.location
是一个公开各种属性的对象

是一种方法;您可以打开新窗口,并可以自定义它。
window.location.href只是当前窗口的一个属性

已经有关于属性和方法的答案了

我将按客观用途:

1.将页面重定向到另一个页面 使用window.location.href。将href属性设置为另一页的href

2.在新窗口或特定窗口中打开链接。 使用window.open()。根据您的目标传递参数

3.知道页面的当前地址 使用window.location.href。获取window.location.href属性的值。您还可以从window.location对象获取特定的协议、主机名、哈希字符串


有关详细信息,请参阅。

窗口。打开将在“新浏览器”选项卡中打开url

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

以下是(在SO snippets窗口中。open不起作用)

var url='1〕https://example.com';
函数go1(){window.open(url)}
函数go2(){window.location.href=url}
函数go3(){location=url}
通过:
打开新窗口
window.location.href

location
标准可能确实说
window.location.href
是一种属性,而不是一种方法,但Internet Explorer(至少版本10)允许您将
href
也视为一种方法。我只在IE10中看到了它的工作原理,在我使用过的一个页面上。这可能就是提问者调用
href
方法的原因。请看问题。但是,是的,最好使用
href
作为属性,它可以在任何浏览器中工作。@RoryO'Kane,这个问题是在2011年提出的。我怀疑用户指的是IE 10.True。但我认为,IE的旧版本很可能(尽管不确定)以同样的方式对待
window.location.href
。毕竟,iE的新版本通常更多地基于标准,而不是更少。因此,如果IE10仍在违反标准,那么旧版本可能也会违反。使用
window.open(newUrl,''u self')
和location.href=newUrl`有什么区别?两者都将在同一选项卡中打开
newUrl
。如果将“\u self”作为附加参数传递,则window.open()也可以在同一窗口中打开“url”。是的,有相同的疑问。使用
window.open(newUrl,'.'u self')
和location.href=newUrl`有什么区别?相关: