Firefox上的window.open(url)和window.location.href = url有什么区别? [英] What's the difference between window.open(url) and window.location.href = url on Firefox?

查看:147
本文介绍了Firefox上的window.open(url)和window.location.href = url有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



然而,我发现这个

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

不起作用,

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

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



这是在Firefox上。有趣的是,在Chrome浏览器上工作正常。



这是一个安全的事情吗? window.open() window.location.href 之间的区别在于 open()是窗口类的方法 window.location


$ b 1。 window.open()是一个窗口类的属性。调用 window.open()方法 >方法实际上创建一个窗口对象,它可以保存在一个变量中,并根据程序的需要进行操作。

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

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

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

2。 window.location 是窗口类中的只读属性

尽管 window.location 是一个只读属性,但是 window.location 有一个内置的快捷方式功能允许 window.location 是可分配的,与调用 window.location.assign() ,它不返回一个窗口对象,而是使用根窗口对象来分配一个新的url,导致新分配的url被加载到浏览器窗口中的JavaScript分配位置被调用。如果你正在创建一个bookmarket脚本,那么使用 window.location 是抓取当前窗口的最好方法网址并将其分配给您的程序的网址字符串。

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

I am trying to build a bookmarklet, feeding the current url as a parameter to another 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.open opens another window, and window.location.href changes location, why does the latter work, while the former just opens another window to the original location?

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

Is this a security thing?

解决方案

The difference between window.open() and window.location.href is that open() is a method of the window class, and window.location is a property of the window class.

1.window.open() is a method on the window class

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.

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

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.

2. window.location is a read-only property on the window class.

Although window.location is a read-only property, the window.location has a built-in shortcut feature that allows window.location to 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.

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

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.

这篇关于Firefox上的window.open(url)和window.location.href = url有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆