即使弹出窗口被阻止,热键插件也会打开新窗口?
如果按下“F2”我想打开新窗口。下面的代码在 Firefox 中给出了 newWindow is null
错误消息。如果我不使用弹出窗口阻止程序,它就可以工作。 IE 中也一样。即使打开了弹出窗口阻止程序,它也可以在 Chrome 中工作。
hotkeys: {
"f3" : function () {
url = "http://www.vse.cz";
var newWindow = window.open(url, '_blank');
newWindow.focus();
return false;
},
Q1: 我可以让它适用于所有浏览器,这样用户就不会使用它使用热键插件时必须更改其设置吗?
Q2:为什么使用JavaScript而不是target打开新窗口 在 Firefox 中工作没有任何问题吗?这是因为它是一个链接并且没有使用热键插件吗?
我的理解是上面页面的脚本以某种方式 操纵发生的事情 当用户单击链接时。它改变了点击的属性 浏览器“不知道”这是新窗口,因此弹出窗口拦截器是 绕过了。
就我而言,我使用由其他东西触发的纯js函数,而不是由 用户点击。并且“我的函数”不会更改任何 html 对象的属性。我认为这就是区别。我不确定我是否是 就在这里。
I want to open new window if "F2" pressed. Below code gives me newWindow is null
error message in firefox. If I don't use pop-up blocker it works. The same in IE. It work in chrome even with pop-up blocker on.
using jstree pre 1.0 stable
hotkeys: {
"f3" : function () {
url = "http://www.vse.cz";
var newWindow = window.open(url, '_blank');
newWindow.focus();
return false;
},
Q1: Can I make it work for all browsers so users don't have to change their settings when using hotkeys plugin?
Q2: How come Using JavaScript instead of target to open new windows works without any troubles in firefox? Is that because it's a link and not using hotkeys plugin?
My understanding is that the script from above page somehow
manipulates what happens
when user clicks a link. It changes the properties of the click so
browsers "don't know" that it's new window so pop-up blocker is
bypassed.
In my case I use pure js function triggered by something else, not by
a user click. And that 'my function' doesn't changes properties of any html objects. I think this is the difference. I am not sure if I am
right here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,您无法通过按键打开新窗口(除了禁用弹出窗口阻止程序)。
IE、Firefox 和 Chrome 中的弹出窗口拦截器的工作方式(从高层次来看)是通过浏览器(在遇到对
window.open
的调用时)遍历 JavaScript 调用堆栈来确定是否当前函数是事件处理程序,或者由事件处理程序的函数调用。换句话说,它会查明当前函数是否正在执行,因为用户执行了触发 DOM 事件的操作。如果是,则允许弹出;否则会被阻止。然而,哪些事件符合“允许弹出窗口”的问题因浏览器而异。 Mozilla 默认情况下,仅
更改
、点击、
dblclick
、mouseup
、reset
和submit
符合条件。 (我认为 IE 是类似的。)作为任何其他类型事件的事件处理程序的函数 - 例如您的
keydown
/keyup
/keypress
case – 不有资格获得特殊的弹出窗口允许处理,这意味着您的弹出窗口被阻止,这就是您对window.open
的调用返回null
的原因。然而,Chrome 确实认为
keydown
事件符合允许打开弹出窗口的条件,这就是您的脚本在该浏览器中运行的原因。这里有一个简化的示例来演示其工作原理。此演示:
spawn()
的函数,该函数调用window.open
打开弹出窗口。spawn()
。由于调用是从全局范围进行的,因此所有浏览器都会阻止此操作;它不是从事件处理程序调用的。window.onkeydown
,该函数调用spawn()
。如果您在 Chrome 中按任意键,就会打开弹出窗口,因为它允许来自keydown
处理程序的弹出窗口。在 IE 和 Firefox 中,弹出窗口将被阻止,因为这些浏览器不允许通过键盘事件弹出窗口。spawn()
的链接。当您单击该链接时,所有浏览器都将允许弹出窗口,因为对window.open
的调用可以追溯到click
事件的事件处理程序。正如您现在所看到的,没有任何操作来操纵事件属性或“欺骗”浏览器不知道有一个新窗口。允许通过链接点击打开弹出窗口的行为是设计使然,其理论是,如果您点击了某个内容,您可能想要看到任何内容在弹出窗口中。但是,当从您未执行任何操作的位置(例如全局范围)调用
window.open
时,您可能没有有任何对自动启动的弹出窗口中的任何[广告]感兴趣。通过这种方式,弹出窗口阻止程序可以防止烦恼(自动启动广告),同时仍然允许页面根据用户的请求打开弹出窗口。
Unfortunately, there's nothing you can do to open a new window on a keypress (other than disabling the popup blocker).
The way that the popup blockers in IE, Firefox and Chrome work (from a high level) is by the browser (upon encountering a call to
window.open
) walking up the JavaScript call stack to determine if the current function is—or was called by a function that is—an event handler. In other words, it finds out if the current function is executing because the user did something that triggered a DOM event.If so, then the popup is allowed; otherwise it is blocked. However, the question of which events qualify as "popup-allowing" vary by browser. By default in Mozilla, only
change
,click
,dblclick
,mouseup
,reset
, andsubmit
qualify. (I assume IE is similar.)Functions that are event handlers for any other type of event – such as
keydown
/keyup
/keypress
in your case – do not qualify for special popup-allowing treatment, which means your popup is blocked and is why your call towindow.open
returnsnull
.Chrome, however, does consider the
keydown
event eligible for allowing popups to be opened, which is why your script works in that browser.Here's a reduced example to demonstrate how this works. This demo:
spawn()
which callswindow.open
to open a popup.spawn()
immediately as the page is loaded. This is blocked by all browsers since the call is made from the global scope; it is not called from an event handler.window.onkeydown
which callsspawn()
. If you press any key in Chrome, the popup will open because it allows popups fromkeydown
handlers. In IE and Firefox, the popup will be blocked becuase those browsers do not allow popups from keyboard events.spawn()
. When you click the link, the popup will be allowed in all browsers because the call towindow.open
can be traced back to an event handler for aclick
event.As you can now see, nothing goes on to manipulate event properties or "trick" the browser in to not knowing that there's a new window. The behavior of popups being allowed to open from link clicks is by design, the theory being that if you've clicked on something, it's likely that you want to see whatever is in the popup. However, when a call is made to
window.open
from a place where you've not done anything (such as the global scope), it's likely you do not have any interest in whatever [ad] is in the automatically-launching popup.In this way, popup blockers prevent annoyances (automatically launching ads) while still allowing pages to open popups at the user's request.