I'm writing a script that when a button is clicked, will check by ajax if the user is logged in, and if so, do some actions without leaving the page. If the user is not logged in, I want to open a login window popup. THe problem is that since window.open() is not directly under the 'click' event, it gets blocked by popup blockers. If I put the window.open right under the click action, it works fine.
Any way around this?
$('.myButton').click(function() {
$.get(ajaxUrl, function(hastoken) {
if(hastoken == 1) {
// do stuff here
} else {
window.open("http://www.google.com");
// login
}
});
});
I suggest using fancybox: ( http://fancybox.net/ )
$('.myButton').click(function() {
$.get(ajaxUrl, function(hastoken) {
if(hastoken == 1) {
// do stuff here
} else {
$("#some_div").fancybox({
'transitionIn' : 'elastic',
'transitionOut' : 'elastic',
'speedIn' : 600,
'speedOut' : 200,
'overlayShow' : false
});
}
});
});
OK, Here's what I came up with, many thanks to this answer . Basically, I wrapped the ajax call in a synchronous wrapper, keeping the windw.open in the right level.
$('.myButton').click(function() {
var hasToken = getResponse(url);
if(hastoken == 1) {
// do stuff here
} else {
window.open("http://www.google.com");
// login
}
});
function getResponse(strUrl) {
var strReturn = "";
jQuery.ajax({
url: strUrl,
success: function(html) {
strReturn = html;
},
async:false
});
return strReturn;
}
This might help you- this will open a kind of light box...
$('.myButton').click(function() {
$.get(ajaxUrl, function(hastoken) {
if(hastoken == 1) {
// do stuff here
} else {
var dialogDiv=$("<div></div>");
var $dialog = $(dialogDiv)
.load("Your Url to login form")
.dialog({
autoOpen: false,
title: "Message Box",
width: 900,
modal: true,
buttons: {
"Close": function() {
$(this).dialog("close");
}
}
});
$dialog.dialog("open");
return false;
}
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.