如何避免浏览器弹出拦截器?

huangapple go评论75阅读模式
英文:

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');

huangapple
  • 本文由 发表于 2023年3月31日 16:15:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定