问题
i have a strange query, i want to open a new window (a popup), when the browser/tab is closed using asp.net, Jquery, but i want to bypass the popup blocker which blocks the window, can anyone help me with this, how can i open a popup when user closes the browser/tab, or any other alternatives which can help me achieve the same. the main problem is i want to ignore the popup blocker. on one the SO Post
i read the below example could help:
jQuery(function($) {
// This version does work, because the window.open is
// during the event processing. But it uses a synchronous
// ajax call, locking up the browser UI while the call is
// in progress.
$("#theButton").click(function(e) {
e.preventDefault();
$.ajax({
url: "http://jsbin.com/uriyip",
async: false,
dataType: "json",
success: function() {
window.open("http://jsbin.com/ubiqev");
}
});
});
});
i replaced the click event with $(window).unload
but that too didnt helped. the popup does not opens, but when i remove e.preventDefault();
the popup opens, but require the popup-blocker be enabled.
回答1:
I don't think there's a way to go around pop-up blockers.
You should change the approach and try to open the content in a jQuery UI modal dialog box, instead of using an actual browser window popup.
回答2:
You have to open the window inside the same function as the $.ajax
, otherwise some browsers will still reject the popup.
jQuery(function($) {
// This version does work, because the window.open is
// during the event processing. But it uses a synchronous
// ajax call, locking up the browser UI while the call is
// in progress.
$("#theButton").click(function(e) {
// use success flag
var success = false;
e.preventDefault();
$.ajax({
url: "http://jsbin.com/uriyip",
async: false,
dataType: "json",
success: function() {
success = true; // set flag to true
}
});
if (success) { // and read the flag here
window.open("http://jsbin.com/ubiqev");
}
});
});
This is the only reliable way to make sure the uriyip
gets called AND popup a window when it's done loading; so it freezes the browser, too bad.
回答3:
Popup blockers are designed to prevent this behavior.
I would suggest using a modal window instead of an actual browser window. I don't think that these get blocked because they are opened within the page itself.
As for the event... You could do something like...
window.onbeforeunload = function whatever() {
//Do code here for your modal to show up.
}
If you are just trying ot give a warning or something you could do
window.onbeforeunload = function showWarning() {
return 'This is my warning to show';
}
来源:https://stackoverflow.com/questions/12626051/opening-popup-from-website-without-popup-blocker-catching-it