Iframe:修订间差异
来自康健生活
| (未显示同一用户的2个中间版本) | |||
| 第40行: | 第40行: | ||
<syntaxhighlight lang="html"> | <syntaxhighlight lang="html"> | ||
<script> | <script> | ||
const onHandleLoad = () => console.log('iframe is loaded'); | const onHandleLoad = () => console.log('iframe is loaded'); | ||
</script> | </script> | ||
<iframe id="iframe" src="https://kangkk.com" onload="onHandleLoad(this)"></iframe></syntaxhighlight> | <iframe id="iframe" src="https://kangkk.com" onload="onHandleLoad(this)"></iframe></syntaxhighlight> | ||
| 第47行: | 第47行: | ||
<syntaxhighlight lang="html"> | <syntaxhighlight lang="html"> | ||
<script> | <script> | ||
document.getElementById('iframe').onload = () => console.log('iframe is loaded'); | document.getElementById('iframe').onload = () => console.log('iframe is loaded'); | ||
</script> | </script> | ||
<iframe id="iframe" src="https://kangkk.com"></iframe></syntaxhighlight> | <iframe id="iframe" src="https://kangkk.com"></iframe></syntaxhighlight> | ||
| 第54行: | 第54行: | ||
<syntaxhighlight lang="html"> | <syntaxhighlight lang="html"> | ||
<script> | <script> | ||
const iframe = document.getElementById('iframe'); | const iframe = document.getElementById('iframe'); | ||
const onHandleLoad = () => console.log('iframe is loaded'); | const onHandleLoad = () => console.log('iframe is loaded'); | ||
iframe.addEventListener('load', onHandleLoad, true); | iframe.addEventListener('load', onHandleLoad, true); | ||
</script> | </script> | ||
<iframe id="iframe" src="https://kangkk.com"></iframe></syntaxhighlight> | <iframe id="iframe" src="https://kangkk.com"></iframe></syntaxhighlight> | ||
| 第63行: | 第63行: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
<script> | <script> | ||
import { fromEvent } from 'rxjs'; | import { fromEvent } from 'rxjs'; | ||
const iframe = document.getElementById('iframe'); | const iframe = document.getElementById('iframe'); | ||
fromEvent(iframe, 'load').subscribe(() => console.log('iframe is loaded'); | fromEvent(iframe, 'load').subscribe(() => console.log('iframe is loaded'); | ||
</script> | </script> | ||
<iframe id="iframe" src="https://kangkk.com"></iframe></syntaxhighlight> | <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> 之后。因此与期望结果不符合。 | |||