Iframe:修订间差异
来自康健生活
创建页面,内容为“== 跳转 == === 向父级跳转 === iframe 在登录页面中加入下面的代码,就会在最外层页面跳转 <syntaxhighlight lang="html5"> <script language…” |
|||
| (未显示同一用户的6个中间版本) | |||
| 第8行: | 第8行: | ||
top.location.href = location.href; | top.location.href = location.href; | ||
</script></syntaxhighlight> | </script></syntaxhighlight> | ||
==== 微信登陆 ==== | |||
在做微信登陆的时候遇到,微信扫描二维码,但是跳转只在 iframe 中跳转,没有跳到父级。 | |||
需要在微信接口配置参数中设置 <code>self_redirect:false</code> | |||
== 事件 == | |||
=== IE === | |||
<syntaxhighlight lang="javascript"> | |||
iframe?.contentDocument?.addEventListener('readystatechange', iframeHandleListener, {passive: false}); | |||
</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 === | |||
<syntaxhighlight lang="javascript"> | |||
iframe?.contentDocument?.addEventListener('load', iframeHandleListener, {passive: false}); | |||
</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> 之后。因此与期望结果不符合。 | |||