代码之家  ›  专栏  ›  技术社区  ›  GhitaB

Sweetalert2:在“是”的“新建”选项卡中打开外部链接

  •  0
  • GhitaB  · 技术社区  · 6 年前

    我使用 SweetAlert2 . 5次页面浏览后,加载页面3秒后弹出“是-否”问题。

    问题是我希望在新的选项卡/窗口中打开外部url。我已经试过了 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 回复  |  直到 6 年前
        1
  •  0
  •   GhitaB    6 年前

    通过将默认按钮替换为链接按钮解决 _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);