英文:
How to avoid browser popup blocker?
问题
As you can see above, I'd like to open a new window when the response is successful. However, the browser's popup blocker doesn't allow a new window to open. How can I fix it?
英文:
async payWithPaypal(context, id) {
context.commit("set_busy", { data: true }, { root: true });
var response = await ApiService.PAY_WITH_PAYPAL(id);
if (response.code == 200 && response.data.success) {
var invoice = response.data.invoice;
AlertUtil.SHOW_SUCCESS_ALERT("Erledigt!");
context.commit("updateInvoice", invoice);
window.open(response.data.redirectUri, "_blank");
} else {
AlertUtil.SHOW_SUCCESS_ALERT(response.data.message);
}
context.commit("set_busy", { data: false }, { root: true });
},
As you can see above, I'd like to open new window when the response is successful.
But the browser popup blocker doesn't allow new window to open.
How can I fix it?
答案1
得分: 0
由于异步操作,出于安全原因,浏览器会阻止它。模拟表单提交可以解决这种问题。
英文:
Due to the asynchronous operation, the browser will block it for security reasons. Simulating form submission can solve such problems。
const openWindow = (url) => {
let form = document.createElement('form');
form.action = url;
form.target = '_blank';
form.method = 'GET';
document.body.appendChild(form);
form.submit();
// clear
document.body.removeChild(form);
form = null;
}
openWindow('https://www.example.com');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论