Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form submit with target="_blank" opens a popup window after ajax request instead new tab

I need some help here...

Anyone knows how to solve this?: http://jsfiddle.net/Q3BfC/5/

When I submit a form with target="_blank" by defaults opens a new tab. But if try to do it after an ajax request, the forms opens a popup window.

(function($) {
    jQuery(document).ready(function() {

        var launch = function(p_url) {
            var form = $('<form />').hide();
            form.attr({'action': p_url, 'target': '_blank'});
            form.appendTo(document.body);
            form.submit();
            form.remove();
            delete form;
        };

        $('#normal').on('click', function() {
            launch('http://www.google.com');
        });

        $('#ajax').on('click', function() {
            $.ajax({
                type: 'POST',
                url: '#',
                traditional: true,
                success: function() {
                    launch('http://www.google.com');
                }
            });
        });

    });
})(jQuery);

Thanks!

like image 627
Marcos Esperón Avatar asked Jan 22 '13 12:01

Marcos Esperón


1 Answers

Its not trying to open it as a popup but its blocked by the popup blocker cause open something with target:_blank in a new tab is only allowed when it is the direct result of an user input, like click or keydown.

You will get the same result when you try to open it in a setTimeout:

setTimeout(function() {launch('http://www.google.com')}, 10);
like image 194
Andreas Köberle Avatar answered Oct 30 '22 10:10

Andreas Köberle