2

I want to open a new tab using JavaScript or jQuery.

I tried this code:

window.open("myurl", '_blank');

But browser gives me alert for pop-up blocked.

I have to open new tab without pop-up blocked alert.

Each and every client can't allow pop-up.

Can anyone help me please?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Shrinivas Mese
  • 69
  • 1
  • 1
  • 8
  • 1
    That's just your browser security. No way around this. ( apart from lowering your security ) – Josh Stevenson May 27 '15 at 10:32
  • 2
    Try searching before asking a question please http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript – George May 27 '15 at 10:32
  • click on the popup blocker and then just check the _always allowed popup from...._ option. – Jai May 27 '15 at 10:34
  • @Jai Each and every client can't allow pop-up. so can i achive this without popup blocker. when we are writing some html code like anchor tag with target is blank then it opens new tab without popup blocker. – Shrinivas Mese May 27 '15 at 10:48

4 Answers4

6

try this,

$('#myButton').click(function () {
    var redirectWindow = window.open('http://google.com', '_blank');
    redirectWindow.location;
});

working js fiddle for this http://jsfiddle.net/safeeronline/70kdacL4/2/

working js fiddle for ajax window open http://jsfiddle.net/safeeronline/70kdacL4/1/

Mohammed Safeer
  • 20,751
  • 8
  • 75
  • 78
5

The only way to overcome this is to perform a synchronous Ajax request which will block your browser while it runs, but will preserve the event context. This will help---> Open new tab without popup blocker after ajax call on user click

Here is the sample code for you --->

<table>

    <tr>
        <td>Works without warning in all browsers:</td>
        <td><input type="button" onclick="performSyncronousRequest()" value="Syncronous request"/><td>
    </tr>

    </tr>
</table>

Scipt--->

/**
* This method will give open the popup without a warning.
*/
function performSyncronousRequest() {
    $.ajax({
     url: '/echo/html',
     data: {},
     success: function(){
         window.open('http://www.w3schools.com');
     },
     async: false
    });
}

Heres the working fiddle http://jsfiddle.net/23JNw/80/

Community
  • 1
  • 1
AkshayJ
  • 771
  • 6
  • 15
  • can you elaborate your explanation or write some code for me? – Shrinivas Mese May 27 '15 at 10:49
  • You can checkout the code.Works like a gem.Cheers :) – AkshayJ May 27 '15 at 10:59
  • i am not using ajax. i want to redirect on new page at the time of page load. so in jquery ready block i had written `window.open("myurl", '_blank');` this code but it shows me pop-up blocker – Shrinivas Mese May 27 '15 at 11:07
  • This is the only workaround to open a path without popup(working cross browser).Using ajax is easy huhh.You can embed an ajax call in your webpage :)Cheers – AkshayJ May 27 '15 at 11:12
  • i created fiddle but not working for me can you suggest changes. [ http://jsfiddle.net/23JNw/81/ ] (http://jsfiddle.net/23JNw/81/) – Shrinivas Mese May 27 '15 at 11:35
  • You will have to include this ajax call on the action you want to open a new tab(in my case its button click);or if you want to open it on load of some web page include this ajax call on load event of that webpage. – AkshayJ May 27 '15 at 11:46
3
var win = window.open('http://stackoverflow.com/', '_blank');
if(win){
    //Browser has allowed it to be opened
    win.focus();
}else{
    //Broswer has blocked it
    alert('Please allow popups for this site');
}
Rasel
  • 5,488
  • 3
  • 30
  • 39
0

You can try like this

$(document).on('click', '.preview', function(event) {
    event.preventDefault();
    if (confirm("Are You Sure?"))
    {
        var daoGroup = $("#daoGroup").val();
        if (daoGroup === undefined && daoGroup === null) {
            alert("Select DAO Groups");
            return false;
        }
        else
        {
            /* Act on the event */
            var data = $(".frmContent").serialize();
            var url = '<?php echo base_url() ?>reportViewPrint/sailorNominalRoll/htmlPreview';
            window.open(url+'?'+ data, '_blank');
        }
    }
});
Bablu Ahmed
  • 4,412
  • 5
  • 49
  • 64