Sweetalert2:在是的新标签页中打开外部链接

时间:2018-04-24 13:14:25

标签: javascript jquery sweetalert2

我使用SweetAlert2实施了此调查弹出窗口。在5次网页浏览后,弹出窗口是 - 在页面加载3秒后无问题。

问题是我希望在新的标签/窗口中打开外部网址。我已经尝试了window.open(survey_url, '_blank');,但它在IE中存在问题。用户必须允许弹出窗口,似乎链接未打开。

在sweetalert2环境中有没有更好的解决方案?我的意思是有一个带链接的简单按钮?

  $(document).ready(function($) {
    var survey_delay = 3000; // milliseconds
    var survey_delay_pageviews = 5;
    var survey_title = "This is a title!";
    var survey_text = "Would you be willing to say Yes?";
    var survey_url = "http://google.com";

    if(!localStorage.getItem("survey_answered")) {
      var pageviews = (+localStorage.getItem("page_views") || 0) + 1;
      localStorage.setItem("page_views", pageviews);

      if(pageviews >= survey_delay_pageviews) {
        setTimeout(function() {
          swal({
            title: survey_title,
            text: survey_text,
            type: "success",
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes',
            cancelButtonText: 'No'
          }).then(function(result) {
            localStorage.setItem("survey_answered", "true");
            window.location.replace(survey_url);
          }).catch(function() {
            localStorage.setItem("survey_answered", "true");
          });
        }, survey_delay);
      }
    }
  });

1 个答案:

答案 0 :(得分:0)

通过使用_blank目标的链接按钮替换默认按钮来解决此问题。在}, survey_delay);行之前添加:

  setTimeout(function () {
    $("button.swal2-confirm").replaceWith('<a target="_blank" class="swal2-confirm swal2-styled" style="background-color: rgb(48, 133, 214); border-left-color: rgb(48, 133, 214); border-right-color: rgb(48, 133, 214);" href="' + survey_url + '">Yes</a>');
    $("a.swal2-confirm").click(function () {
      localStorage.setItem("survey_answered", "true");
      swal.close()
    })
  }, 400);