// 異步Ajax回調中調用
$.ajax({
url: '/check',
success: function(data) {
if (data.allow) {
window.open('/target'); // 被攔截或新窗口
}
}
});
瀏覽器安全策略:只有用戶直接交互事件(click/keydown等)觸發的window.open才被視為可信。Ajax回調屬於異步任務,調用棧斷裂,瀏覽器判定為「腳本主動彈窗」→ 攔截或強制新窗口。
window.open(url, '_blank', 'width=800,height=600'); // ✅ 明確要求彈出窗口
window.open(url, '_blank'); // ✅ 瀏覽器決策:現代多為新標籤
window.open(url); // ⚠️ 同 _blank,但舊瀏覽器可能新窗口
$.ajax({
url: '/check',
async: false, // 同步
success: function(data) {
if (data.allow) window.open('/target');
}
});
// 用戶點擊瞬間打開空白頁
var win = window.open('', '_blank');
// 立即獲得window引用,不被攔截
$.ajax({
url: '/check',
success: function(data) {
if (data.allow) win.location.href = '/target';
else { win.close(); alert('無權限'); }
}
});
function openChecked(url) {
var form = document.createElement('form');
form.method = 'GET';
form.action = url;
form.target = '_blank';
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
// 用戶點擊綁定
btn.onclick = function() {
// 這裡可以先同步檢查或直接提交
// 後端驗證失敗返回錯誤頁
openChecked('/target');
};
window.open(url, '_blank')location.hrefwindow.openfeatures(除非確要彈出窗口)