Why does Google Chrome's popup blocker shoot down a link when it contains an image or any other tag?
Asked Answered
C

1

7

Google Chrome is a bit weird nowadays in that v25 no longer respects a 302 redirect header and happily re-posts form data on page refresh, whereas v24 and all the other browsers play nicely. I'm not sure if this is just a temporary browser bug, so let me describe the mystery I'm trying to solve.

As far as I know you cannot open a tab using HTML or JavaScript, but a new window without explicitly set dimensions is going to open as a tab by default.

First I've tried this:

<a href="URL" onclick="window.open(this.href);return false">
  <img src="NICE IMAGE" alt="foo">
</a>

This was working everywhere, except in Chrome where it was shot down as an unauthorized popup window. Interestingly, sometimes when I kept clicking on it, Chrome has changed its mind and let the link open anyway (as tab). This was not always the case though, sometimes the link remained dead.

After a couple of hours of experimenting (and having changed from onclick to a simple target="_blank") I have discovered that the reason the link was killed is that the click event started from the <img> tag within the <a>. When I replaced the <img> with a <span> that contained some text and clicked on the <span>, the link could not open (Chrome has identified it as an unwanted popup), but when I clicked on the <a> tag itself (that had a fixed size or a padding), then it was accepted, and a new tab was born.

Finally I just included the images as CSS background and the <a> tags remained empty. Everything seems to be working now in every browser with this markup:

<a href="URL" target="_blank" style="background:url('NICE_IMAGE'); height:XXX; width:XXX" title="Description"></a>

What could be the reason behind the Google Chrome’s logic that when a link contains a tag, then it is forbidden from being opened as a new tab, but when it is empty, then it can go ahead?

Chemisorb answered 21/3, 2013 at 16:28 Comment(4)
My guess is Chrome is trying to block some of the common ways web users are "fooled" into clicking a link that ends up as a popup. Mainly when a site creates a full page transparent image that's a link. You don't see it but you click it when you're trying to click an underlying element. But this is just a guess...Goalkeeper
Do you have any other events bound to any of the affected tags? This might affect the behavior of the popup blocker. It would be very helpful if you could provide a demo page.Shamanism
Also, please activate the Event Listener Breakpoints in the Inspector (under Sources) and let us know what types of events are fired when you click on the element.Shamanism
I am having a similar problem using except using pure javascript. When the window.open is called from the same onclick event that was fired it works fine. But if do some kind of async operation and then call window.open it blocks it as a popup. My guess is that chrome thinks its some malicious script trying to open tabs without the user's permission if the call is not initiated directly from the <a> onclick event.Escalante
S
3

After some testing:

  • Chrome Version - 25.0.1364.172 m : worked fine!
  • Chrome Version - 26.0.1410.43 m : worked fine!

the "normal" way to open a tab is working in both newer versions. Tested!

my code:

<?php
echo '
     <a href="http://www.google.com" onclick="window.open(this.href); return false;">
       <img src="http://static.adzerk.net/Advertisers/2565.png" alt="foo" />
     </a>
     ';
?>

the problem should be in your pages...

EDIT: BUG SEARCH i searched for reported and fixed bugs in this issue but couldnt find anything.

Seminole answered 29/3, 2013 at 17:40 Comment(3)
Same here, works in Chromium Version 25.0.1364.160 Ubuntu 13.04 (25.0.1364.160-0ubuntu1b1)Rockling
The same issue has reappeared recently on a different site that I was working on and it has been discovered that a 3rd party analytics JavaScript library had a weird ninja bug.Chemisorb
same as above, adobe analytics lib causing thisSkyway

© 2022 - 2024 — McMap. All rights reserved.