[FE] 被动检测 iframe 加载 src 成功失败的一种思路和方式 (Vue)

思路:设置定时器一个,n 秒后设置 404 或其它,此时给 iframe 的 onload 事件设置回调函数,加载完成则取消定时器。

示例:

data () {
  return {
    handler: null
  }
}

created () {
    this.handler = setTimeout(() => {
      const iframe = document.querySelector('#iframe_id')
      iframe.src = './404'
    }, 3000)
}

mounted () {
    const self = this
    const iframe = document.querySelector('#iframe_id')

    iframe.onload = function () {
      self.iframeCompleteLoad = true

      clearTimeout(self.handler)
    }
}

Link:https://www.cnblogs.com/farwish/p/14407369.html

原文地址:https://www.cnblogs.com/farwish/p/14407369.html