Heim  >  Artikel  >  Web-Frontend  >  file模式访问网页时iframe高度自适应解决方案_javascript技巧

file模式访问网页时iframe高度自适应解决方案_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:43:361029Durchsuche

最近做到iframe的高度自适应这个问题,网上一搜好多解决方案,而总结起来也就那几种,我逐一尝试这些方案,最后发现在我的项目中都不起作用,后来发现自己做的网页是通过file方式访问的,将网页代码放到apache下通过http协议访问,在iframe加载的时候调用如下js方法:

复制代码 代码如下:

functionSetWinHeight(obj)
{
varwin=obj;
if(document.getElementById)
{
if(win&&!window.opera)
{
if(win.contentDocument&&win.contentDocument.body.offsetHeight)
win.height=win.contentDocument.body.offsetHeight;
elseif(win.Document&&win.Document.body.scrollHeight)
win.height=win.Document.body.scrollHeight;
}
}
}

果然网页高度能够自适应(对于其他方案应该也有效果,我没有注意去尝试),而通过file方式访问网页同样的的代码log中始终报:UnsafeJavaScriptattempttoaccessframewithURLfile:///E:/workspace/html5/exam.htmlfrom
framewithURLfile:///E:/workspace/html5/quiz_preview.html.
Domains,protocolsandportsmustmatch.
每种方案都尝试了报的错是这个,只能从这个问题下手解决了,后来发现这种通过file方式跨域访问的方式chrome是默认不允许的,又查如何设置chrome允许跨域访问:最简单的方法在chrome图标右键、属性->目标在后添加‘--disable-web-security'

重新启动chrome打开要调的网页,问题果然解决了
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn