通过按钮点击事件,使用iframe来显示不同的内容(跨域)。
iframe链接内容的服务器未开启的时候,页面显示404, 用户体验太差。
服务器未开启的时候,显示页面需要友好。
代码如下:
btnclick(elem){
this.fullscreenLoading_all = true; //显示加载框
this.ajaxError = false;
if(elem.URL == this.ifram_src){
this.fullscreenLoading_all = false;
return false;
}
//等到iframe数据加载出来之后再进行关闭加载框
let elurl = elem.URL;
this.ifram_src = elurl;
this.$nextTick(() => {
const iframeElem = this.$el.querySelector("#iframe_id");
//5s iframe 无任何加载内容,就任务iframe加载的连接的服务器未连接
this.serveTime = setTimeout(() => {
this.ajaxError = true;
this.fullscreenLoading_all = false;
}, 5 * 1000);
if(iframeElem.attachEvent){
iframeElem.attachEvent('onload', () => {
clearTimeout(this.serveTime);
this.fullscreenLoading_all = false;
})
}else{
iframeElem.onload = () => {
clearTimeout(this.serveTime);
this.fullscreenLoading_all = false;
}
}
})
},
其中注意:
if(elem.URL == this.ifram_src){
this.fullscreenLoading_all = false;
return false;
}
一定要使用returan false ; 不要让代码再进行执行了。
以上全部。 如果更好的办法解决,请留言。