8

I have this JS code:

window.open(loginurl, '_blank');

coming from a condition for example:

if (userloggedin) {

//popup another page

} else {

window.open(loginurl, '_blank');

}

"loginurl" is an login URL that I would like to open in new window.

Problem: This will be blocked in most browsers (Firefox and Chrome) because it behaves like a popup.

I would like a solution that will still utilizes my login URL variable (not altering the if else statement), open it in new window without any warnings of a popup that is being blocked.

I am searching for ways but I never found a solution. If somebody can provide some tips or insights. That would be highly appreciated.

Thanks.

Emerson Maningo
  • 2,189
  • 9
  • 32
  • 47
  • 1
    this should not be blocked if a user clicks somewhere on page, e.g. on a link and opens a popup using this link... or you might need to get into some black-hat JS/FLASH hacking – Zathrus Writer Feb 01 '13 at 09:21
  • 2
    http://stackoverflow.com/questions/7139103/open-page-in-new-window-without-popup-blocking Duplicate question – yajay Jul 02 '13 at 10:15

2 Answers2

12

The window opened by window.open will always be regarded as a pop-up to block by browsers when the function is triggered without a user action initiating it.

That means that for example that these will not be blocked:

$('a').on('click.open', function(e) { e.preventDefault(); window.open('http://disney.com') });

Chrome even allows other events to trigger the popup, while firefox will not allow this:

$(document).on('keydown', function(e) { window.open('http://stackexchange.com') });

And this will be blocked:

$(document).ready(function() { window.open('http://stackoverflow.com') });

So, unless you're triggering the window.open after a user action, you can never be sure that your window won't be blocked.

Beat Richartz
  • 9,474
  • 1
  • 33
  • 50
  • should `$('a').on('click.open'` just be `$('a').on('click'` - I've done a quick search and not come across anything like `click.open` as an event in jQuery (although your example does work)? – Barry Kaye Jun 04 '13 at 08:42
  • 2
    @BarryKaye 'click.open' is a namespaced click event handler. ('click' is the event and 'open' the namespace. That means e.g. if you want to unbind it without unbinding any other click events on the same element, you can do so by calling `$('a').off('click.open')`. – Beat Richartz Jun 04 '13 at 13:20
0

This way won't block your popup:

$.ajax({
    url:"url",
    dataType: "text",
    cache: false,
    success: function(data){
        window.open('url','_self');
   }
});
Aqua Huang
  • 129
  • 1
  • 1