Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hotkey plugin opens new window even if pop-ups are blocked?

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.

like image 604
Radek Avatar asked May 23 '11 08:05

Radek


People also ask

Why am I still getting pop-ups when my pop-up blocker is on?

If you still get pop-ups after disabling them: You may have previously subscribed to receive notifications from a site. You can block notifications if you don't want any communications from a site to show up on your screen. Your computer or phone may be infected by malware.

Why does a pop-up blocker not block all pop-ups automatically?

Malware is able to work around Chrome's pop-up blocker and launch pop-up windows, even if pop-ups are disabled and the site is not included in the exceptions list. It's a sign that the computer has a malware infection if pop-ups are showing up on sites when the blocker should be stopping them.

How do I override a pop-up blocker?

To disable the pop-up blocker, uncheck the Blocked (recommended) box. To enable pop-ups on specific sites, check Blocked (recommended) and click Add next to Allow and enter the URL(s). CHROME (MAC) By default, Chrome will notify you when a pop-up is blocked and give you an option to see it.

How do I allow a pop-up window blocker?

To allow pop-ups: Click the ellipsis icon (...) in the upper right corner of your web browser, and then click Settings. In the Advanced settings section, click View advanced settings. In the Block pop-ups section, click the switch to Off.


1 Answers

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 calls window.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 calls spawn(). If you press any key in Chrome, the popup will open because it allows popups from keydown 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 to window.open can be traced back to an event handler for a click 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.

like image 136
josh3736 Avatar answered Oct 29 '22 03:10

josh3736