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 browser开发者_开发知识库s "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.
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
, and submit
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 to window.open
returns null
.
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:
- Defines a function called
spawn()
which callswindow.open
to open a popup. - Calls
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. - Attaches a function to
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. - Attaches an event handler to the link which calls
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.
精彩评论