mas*_*sif 252 javascript location window href window-object
JavaScript中的方法window.location.href
和window.open ()
方法有什么区别?
Jam*_*ill 479
window.location.href
是不是一种方法,它是一个属性,它会告诉你浏览器的当前URL位置.更改属性的值将重定向页面.
window.open()
是一种方法,您可以将URL传递给要在新窗口中打开的URL.例如:
window.location.href示例:
window.location.href = 'http://www.google.com'; //Will take you to Google.
Run Code Online (Sandbox Code Playgroud)
window.open()示例:
window.open('http://www.google.com'); //This will open Google in a new window.
Run Code Online (Sandbox Code Playgroud)
window.open()
可以传递其他参数.请参阅:window.open教程
Tom*_*Tom 32
window.open
将打开具有指定URL的新浏览器.
window.location.href
将在调用代码的窗口中打开URL.
还要注意,它window.open()
是窗口对象本身的函数,而是window.location
一个公开各种其他方法和属性的对象.
Som*_*luk 12
已经有答案描述了window.location.href属性和window.open()方法.
我会按目标使用:
使用window.location.href.将href属性设置为另一个页面的href.
使用window.open().根据您的目标传递参数.
使用window.location.href.获取window.location.href属性的值.您还可以从window.location对象获取特定协议,主机名,哈希串.
有关更多信息,请参阅位置对象
window.open ()
将打开一个新窗口,而window.location.href
在当前窗口中打开新URL.