Skip to content Skip to sidebar Skip to footer

Javascript Window.open Works In Chrome Console But Not As A Snippet

I'm relatively new to Javascript and I'm working on some little scripts to modify webpages that I work with often. One of the things I'm automating is opening certain links in tabs

Solution 1:

When Chrome detects a call to window.open() which does not originate from a user gesture, it blocks it as a pop-up.

You can manually override this via the little Pop-Up modal that shows up on the right-edge of your URL bar:

pop-up modal

The interesting thing you brought up is: why does it work from the Console, but not from Snippets? I asked the DevTools team, and one of them informally thinks that DevTools treats Console execution as user gestures, to avoid security checks like these.

One workaround that may be good enough for you is to run your snippet from a new Chrome tab (chrome://newtab). When I run the Snippet from that page, I can open new windows without triggering the "Pop-up blocked" modal.

Thanks for experimenting between how it worked with Console and Snippets. That turned out to be pretty interesting.


Solution 2:

Indeed, window.open is kind of misleading. This is to the fact that window is the global context in JavaScript and open is a method within that object. You should not read it as "open a window". Actually, it

loads a resource into [...] a new browsing context (such as a window)

(MDN)

Most modern browsers are tabbed, hence a browsing contxt is a tab and, following of that, they (mostly, but not always) would open a new tab rather than a window. What can you do? For example, code like

window.open('http://google.de', 'google', 'width=500,height=500')

actually opens a window for me (Chrome 59). However, it might get blocked as a popup by the browser.


Post a Comment for "Javascript Window.open Works In Chrome Console But Not As A Snippet"