如何open一个新tab页面

打开新tab页的两种方式

1 a标签

  function openwin(url) {
    var a = document.createElement("a");
    a.setAttribute("href", url);
     a.setAttribute("target", "_blank");
     a.setAttribute("id", "camnpr");
     document.body.appendChild(a);
     a.click();
  }

2 window.open

window.open('https://sandbox.ebanx.com/print/?hash=59ad5dd18a6d5ba0e24327c2ba92a730115a80bd58b3baa5', '_blank')

有3种情况会需要打开新tab页,

  • 人为点击一个按钮,在事件里我们可以打开新的tab页,window。open()
  • 用户直接点击a标签打开新tab页
  • 用户触法的ajax回调,在回调事件里才能拿到新的需要跳转的tab页的url,此时以上方法打开新页面时候回被chrome等游览器默认拦截

解决方案:

function click() {

    var newWin = window.open('loadingurl');
      $.ajax({
          url: url,
          type: "post",
          data: payParams,
          dataType: 'json',
          success: function (response) {
            newWin.location = response.data.url
        }
    })

}

就是在点击的时候 先打开一个默认的loading页面 然后在等url回来后在赋值给location

以上

猜你喜欢

转载自www.cnblogs.com/WhiteHorseIsNotHorse/p/9076759.html
今日推荐