window.open 被瀏覽器攔截

在項目開發(fā)中, 需要在按鈕點擊后發(fā)出一個ajax請求并在請求完成后, 彈出一個新窗口頁面. 自然想到用window.open實現(xiàn), 但會被瀏覽器攔截

分析

當(dāng)瀏覽器檢測到非用戶操作產(chǎn)生的新彈出窗口,則會對其進(jìn)行阻止。因為瀏覽器認(rèn)為這可能是一個廣告,不是一個用戶希望看到的頁面。

當(dāng)window.open為用戶觸發(fā)事件內(nèi)部或者加載時,不會被攔截,一旦將彈出代碼移動到ajax或者一段異步代碼內(nèi)部,馬上就出現(xiàn)被攔截的現(xiàn)象。

被攔截示例代碼

例如, 下面這段代碼就會被攔截:

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <h3>{{msg}}</h3>
    <button ref='btn' type="button" @click="clickHandler">click</button>
  </div>
</template>
<script>
export default {
  props: {
    msg: {
      required: true,
      type: String
    }
  },
  data () {
    return {
      url: 'http://www.baidu.com/',
    }
  },
  mounted () {
    setTimeout(() => {
      window.open(this.url, '_blank')
    }, 100)
  }
}
</script>

解決方案

1. 打開新窗口的代碼綁定到click的事件回調(diào)中,就可以避免大部分瀏覽器對窗口彈出的攔截:

如下示例代碼:

button的click事件觸發(fā)的回調(diào)函數(shù)clickHandler內(nèi)打開一個新窗口不會被瀏覽器攔截.

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <h3>{{msg}}</h3>
    <button ref='btn' type="button" @click="clickHandler">click</button>
  </div>
</template>
<script>
export default {
  props: {
    msg: {
      required: true,
      type: String
    }
  },
  data () {
    return {
      url: 'http://www.baidu.com/',
    }
  },
  mounted () {
    
  },
  methods: {
    clickHandler () {
      window.open(this.url, '_blank')
    }
  }
}
</script>

2. 通過form表單提交實現(xiàn)(親測, 會被攔截)

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <h3>{{msg}}</h3>
    <form action="http://www.baidu.com" ref="form" method="get" target="_blank" style="display: none"></form>
  </div>
</template>
<script>
export default {
  props: {
    msg: {
      required: true,
      type: String
    }
  },
  data () {
    return {
      url: 'http://www.baidu.com/',
    }
  },
  mounted () {
    setTimeout(() => {
      this.$refs.form.submit()
    }, 1000)    
  },
  methods: {

  }
}
</script>

將上面的代碼改造下, form提交的事件由button按鈕的click事件觸發(fā)的話, 就不會被瀏覽器攔截

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <h3>{{msg}}</h3>
    <form action="http://www.baidu.com" ref="form" method="get" target="_blank" style="display: none"></form>
    <button type="button" @click='clickHandler'>button</button>
  </div>
</template>
<script>
export default {
  props: {
    msg: {
      required: true,
      type: String
    }
  },
  data () {
    return {
      url: 'http://www.baidu.com/',
    }
  },
  mounted () {},
  methods: {
    clickHandler () {
      this.$refs.form.submit()
    }
  }
}
</script>

3. 先彈出窗口,然后重定向(最優(yōu)方案)

先通過用戶點擊打開頁面(必須是用戶點擊而觸發(fā)的行為),然后再對頁面進(jìn)行重定向

<template>
  <div class="about">
    <h1>This is an about page</h1>
    <h3>{{msg}}</h3>
    <button ref="btn" @click="clickHandler">button</button>
  </div>
</template>
<script>
export default {
  props: {
    msg: {
      required: true,
      type: String
    }
  },
  data () {
    return {
      url: 'http://www.baidu.com/',
      newTab: null
    }
  },
  mounted () {},
  methods: {
    clickHandler () {
      this.newTab = window.open('about:blank')
      let $ajax = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve()
        }, 1000)
      })   
      $ajax.then(() => {
        this.newTab.location.href = this.url
        setTimeout(() => {
          this.newTab = null
        }, 1)
      })
    }
  }
}
</script>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 留梗,有關(guān)于創(chuàng)傷后遺癥的現(xiàn)實與噩夢。 第一次還是給我的姑娘們好了,受過傷的少女因為痛苦而輾轉(zhuǎn)難眠,現(xiàn)實究竟算是終止...
    謝作人閱讀 202評論 0 0
  • 感悟:經(jīng)營美樂家要有老板的心態(tài),愿意復(fù)制,肯學(xué)習(xí),有目標(biāo),積極主動,能配合團(tuán)隊,有堅持的決心。
    葉凡02閱讀 647評論 0 0
  • 牛人之所以更牛,其中有個很重要的一點他不論在學(xué)習(xí)和踐行當(dāng)中,他們思考比別人多幾步,甚至更多。例如:下象棋,一般入門...
    水中望我閱讀 253評論 0 1
  • 10.24快樂痛苦四原則 多個好消息一起說,能遞增喜悅:多個壞消息一起說,能減少不快。
    張琪77閱讀 143評論 0 0
  • 從錦里出來,打車不易的我們選擇了一輛三輪車,老師傅一口說要25,我們嫌有些貴,最后以議價到20,一路上總是忐忑,三...
    萍香閱讀 261評論 0 2

友情鏈接更多精彩內(nèi)容