javascript Firefox 上的 window.open(url) 和 window.location.href = url 有什么区别?

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

What's the difference between window.open(url) and window.location.href = url on Firefox?

javascriptfirefox

提问by simone

I am trying to build a bookmarklet, feeding the current url as a parameter to another url.

我正在尝试构建一个书签,将当前 url 作为参数提供给另一个 url。

However I am finding out that this

但是我发现这

javascript:(function(){window.open("http://www.somesi.te/some/thing?url="+encodeURIComponent(window.location.href))})()

does not work, while this

不起作用,而这

javascript:(function(){window.location.href = "http://www.somesi.te/some/thing?url="+encodeURIComponent(window.location.href)})()

does. Apart from the obvious difference that window.openopens another window, and window.location.hrefchanges location, why does the latter work, while the former just opens another window to the original location?

做。除了window.open打开另一个窗口并window.location.href更改位置的明显区别之外,为什么后者起作用,而前者只是打开另一个窗口到原始位置?

This is on Firefox. Funnily enough, on Chrome things work fine.

这是在 Firefox 上。有趣的是,在 Chrome 上一切正常。

Is this a security thing?

这是安全问题吗?

回答by radiovisual

The difference between window.open()and window.location.hrefis that open()is a methodof the window class, and window.locationis a propertyof the window class.

之间的差window.open()window.location.hrefopen()是一个方法窗口类的,和window.location是一个属性窗口类的。

1.window.open()is a methodon the window class

1.window.open()是window类上的一个方法

Calling the window.open()method actually creates a window object, which can be held in a variable and manipulated according to your program's requirements.

调用该window.open()方法实际上会创建一个窗口对象,该对象可以保存在一个变量中并根据您的程序要求进行操作。

To demonstrate that window.open() actually returns a window object, consider the following code:

为了演示 window.open() 实际上返回一个 window 对象,请考虑以下代码:

var mywindow = window.open("http://google.com");
mywindow.name = "Awesome Window";
console.log(typeof(mywindow)); // --> "object"
console.log(mywindow.name); // --> "Awesome Window"

The reason your code was opening an unwanted window, is because you were calling window.open(), whose sole purpose in life is to open a new window.

您的代码打开一个不需要的窗口的原因是因为您正在调用window.open(),其生命的唯一目的是打开一个新窗口。

2. window.locationis a read-only propertyon the window class.

2.window.location是window类上的只读属性

Although window.locationis a read-only property, the window.locationhas a built-in shortcut feature that allows window.locationto be assignable, which has the same effect as calling window.location.assign(), which does not return a window object, but uses the root window object to assign a new url to, causing the newly-assigned url to be loaded in the browser window where the javascript assigning the location was called.

虽然window.location是只读属性,但是window.location内置了允许window.location可赋值的快捷功能,与调用的效果一样window.location.assign(),不返回窗口对象,而是使用根窗口对象分配一个新的url给,导致新分配的 url 加载到浏览器窗口中,在该窗口中调用分配位置的 javascript。

If you are creating a bookmarket script, then using window.locationis the better way of grabbing the current window's url and assigning it to your program's url string.

如果您正在创建一个书市脚本,那么 usingwindow.location是获取当前窗口的 url 并将其分配给您程序的 url 字符串的更好方法。

The reason why you might find that you are getting unexpected behavior in different browsers, is that there is no official public standard set for the window object, so how each browser chooses to implement it behind the scenes may vary.

您可能会发现在不同浏览器中出现意外行为的原因是没有为 window 对象设置官方公共标准,因此每个浏览器选择如何在幕后实现它可能会有所不同。