Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 953
Wabbitseason Avatar asked Mar 21 '13 16:03

Wabbitseason


1 Answers

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.

like image 152
Shlomi Hassid Avatar answered Sep 26 '22 17:09

Shlomi Hassid