Chrome 浏览器下iframe标签的src属性二次加载(onload事件)怎么避免?

现在是要把人家的第三方系统内嵌到我方系统中 用的是获取code免密码登录的场景 那个code在返回的url中 。同一个code也就是同一个url 只允许访问一次登录 之后的访问会提示code无效。

如果按网上的说法(参考:https://www.cnblogs.com/yjken...)在通过document.CreateElement创建iframe等其加载进dom后,再给src赋值 会引起无限循环。

Firefox没这种现象 。

有大佬了解这个点吗或者有其他替代方案吗?

       var iframe = document.createElement("iframe");
        var height = document.documentElement.clientHeight;
        iframe.height = height;
        iframe.name = "iframe";
        iframe.id = "iframe";
        iframe.width = "100%";
        iframe.frameborder = "0";
        //挂载到DOM上
        document.getElementById("iframeDiv").appendChild(iframe);
        iframe.src = this.url;
          // iframe.contentDocument.body.onload = function(){
        //   console.log('加载完成');
        // }
        iframe.onload = () => {
          console.log('加载完成2');
          //document.getElementById("iframe").src = this.url;
          // iframe.contentWindow.location.reload(false);
        }

问题补充:

        iframe.onload = (evt) => {
          this.flag++;
          console.log('加载完成---'+this.flag);
          console.log("src = '" + evt.target.src + "'");
          console.log(evt)
          if(this.flag===1){
           // document.getElementById("iframe").contentWindow.history.back();
            //evt.target.src=this.url;
            iframe.onload =null;
          }
          //document.getElementById("iframe").src = this.url;
          // iframe.contentWindow.location.reload(false);
       }

1.本次调用的url接口会引发chrome 的iframe标签src 二次onload。但同样的代码,火狐却不会有二次onload的情况。而且chrome下其他url链接也不会让iframe二次onload
2.接口提供方说这个接口会有redirect
3.iframe.onload =null;只能监听加载完成,并不能阻止它不进行二次加载的行为。这一个也许有异议,但我这边运行情况确实如此

阅读 4.4k
1 个回答

onload 执行之后,你 this.onload = null 不就完事了吗?不就是只执行一次嘛?用这么费劲?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题