Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

javascript - Allow window.open to open new window and not popup

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...