window.open 被浏览器拦截

在项目开发中, 需要在按钮点击后发出一个ajax请求并在请求完成后, 弹出一个新窗口页面. 自然想到用window.open实现, 但会被浏览器拦截

分析

当浏览器检测到非用户操作产生的新弹出窗口,则会对其进行阻止。因为浏览器认为这可能是一个广告,不是一个用户希望看到的页面。

window.open为用户触发事件内部或者加载时,不会被拦截,一旦将弹出代码移动到ajax或者一段异步代码内部,马上就出现被拦截的现象。

被拦截示例代码

例如, 下面这段代码就会被拦截:

<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的事件回调中,就可以避免大部分浏览器对窗口弹出的拦截:

如下示例代码:

button的click事件触发的回调函数clickHandler内打开一个新窗口不会被浏览器拦截.

<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表单提交实现(亲测, 会被拦截)

<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事件触发的话, 就不会被浏览器拦截

<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. 先弹出窗口,然后重定向(最优方案)

先通过用户点击打开页面(必须是用户点击而触发的行为),然后再对页面进行重定向

<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>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 留梗,有关于创伤后遗症的现实与噩梦。 第一次还是给我的姑娘们好了,受过伤的少女因为痛苦而辗转难眠,现实究竟算是终止...
    谢作人阅读 186评论 0 0
  • 感悟:经营美乐家要有老板的心态,愿意复制,肯学习,有目标,积极主动,能配合团队,有坚持的决心。
    叶凡02阅读 607评论 0 0
  • 牛人之所以更牛,其中有个很重要的一点他不论在学习和践行当中,他们思考比别人多几步,甚至更多。例如:下象棋,一般入门...
    水中望我阅读 230评论 0 1
  • 10.24快乐痛苦四原则 多个好消息一起说,能递增喜悦:多个坏消息一起说,能减少不快。
    张琪77阅读 128评论 0 0
  • 从锦里出来,打车不易的我们选择了一辆三轮车,老师傅一口说要25,我们嫌有些贵,最后以议价到20,一路上总是忐忑,三...
    萍香阅读 229评论 0 2