Iframe:修订间差异
来自康健生活
| (未显示同一用户的4个中间版本) | |||
| 第15行: | 第15行: | ||
=== IE === | === IE === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
iframe?.contentDocument?.addEventListener('readystatechange', iframeHandleListener, {passive: false}); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
eq: | |||
<syntaxhighlight lang="javascript"> | |||
//check for IE | |||
if (navigator.userAgent.indexOf("MSIE") > -1 && !window.opera){ | |||
iframe.onreadystatechange = function(){ | |||
if (iframe.readyState == "complete"){ | |||
alert("Iframe is loaded."); | |||
} | |||
}; | |||
} else { | |||
iframe.onload = function(){ | |||
alert("Iframe is loaded."); | |||
}; | |||
}</syntaxhighlight> | |||
=== 非 IE === | === 非 IE === | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
iframe?.contentDocument?.addEventListener('load', iframeHandleListener, {passive: false}); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== load == | |||
=== inline javascript === | |||
<syntaxhighlight lang="html"> | |||
<script> | |||
const onHandleLoad = () => console.log('iframe is loaded'); | |||
</script> | |||
<iframe id="iframe" src="https://kangkk.com" onload="onHandleLoad(this)"></iframe></syntaxhighlight> | |||
=== javascript === | |||
<syntaxhighlight lang="html"> | |||
<script> | |||
document.getElementById('iframe').onload = () => console.log('iframe is loaded'); | |||
</script> | |||
<iframe id="iframe" src="https://kangkk.com"></iframe></syntaxhighlight> | |||
=== addEventListener === | |||
<syntaxhighlight lang="html"> | |||
<script> | |||
const iframe = document.getElementById('iframe'); | |||
const onHandleLoad = () => console.log('iframe is loaded'); | |||
iframe.addEventListener('load', onHandleLoad, true); | |||
</script> | |||
<iframe id="iframe" src="https://kangkk.com"></iframe></syntaxhighlight> | |||
=== Observables === | |||
<syntaxhighlight lang="javascript"> | |||
<script> | |||
import { fromEvent } from 'rxjs'; | |||
const iframe = document.getElementById('iframe'); | |||
fromEvent(iframe, 'load').subscribe(() => console.log('iframe is loaded'); | |||
</script> | |||
<iframe id="iframe" src="https://kangkk.com"></iframe></syntaxhighlight> | |||
== 实例 == | |||
=== react === | |||
在一个 react 项目中,又一个 <code>iframe</code> 标签,其 <code>src</code> 是动态变化的,需求的预期是当 <code>iframe</code> 加载新的页面时,在 <code>iframe</code> 内部加载 <code>js</code> 之前,执行一个 <code>js</code> 语句,改变它的 <code>userAgent</code>。所以我做了如下: | |||
<syntaxhighlight lang="javascript"> | |||
useEffect(() => { | |||
if (!iframeRef.current || !isMac()) return; | |||
function setUserAgent(_window, _userAgent) { | |||
if (_window.navigator.userAgent != _userAgent) { | |||
var userAgentProp = { get: function () { return _userAgent; } }; | |||
try { | |||
Object.defineProperty(_window.navigator, 'userAgent', userAgentProp); | |||
} catch (e) { | |||
_window.navigator = Object.create(_window.navigator, { | |||
userAgent: userAgentProp | |||
}); | |||
} | |||
} | |||
} | |||
try { | |||
setUserAgent( | |||
iframeRef.current.contentWindow || iframeRef.current.contentDocument, | |||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.355' | |||
); | |||
} catch(e) { | |||
setUserAgent( | |||
iframeRef.current.contentWindow || iframeRef.current.contentDocument, | |||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.355' | |||
); | |||
} | |||
}, [iframeRef.current, pageURL]);</syntaxhighlight> | |||
这段代码在开发环境可满足预期,但是生产环境不行,我的理解是 <code>iframe</code> 标签的 <code>onLoad</code> 结束和 react 的监听 pageURL 后执行的代码是并行异步的,是竟态关系。在生产环境下,<code>onLoad</code> 会更快结束,所以代码依然执行了,但是在 <code>onLoad</code> 结束之后,也就是完全加载完 <code>iframe</code> 的 <code>js</code> 之后。因此与期望结果不符合。 | |||