admin管理员组

文章数量:1220818

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.

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.

Share Improve this question asked Feb 1, 2013 at 9:19 Emerson ManingoEmerson Maningo 2,2699 gold badges33 silver badges48 bronze badges 2
  • 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 Commented Feb 1, 2013 at 9:21
  • 2 stackoverflow.com/questions/7139103/… Duplicate question – yajay Commented Jul 2, 2013 at 10:15
Add a comment  | 

2 Answers 2

Reset to default 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.

This way won't block your popup:

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

本文标签: javascriptAllow windowopen to open new window and not popupStack Overflow