Iframe:修订间差异
来自康健生活
| 第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> | ||
2022年2月24日 (四) 10:22的版本
跳转
向父级跳转
iframe 在登录页面中加入下面的代码,就会在最外层页面跳转
<script language="JavaScript">
if (window != top)
top.location.href = location.href;
</script>
微信登陆
在做微信登陆的时候遇到,微信扫描二维码,但是跳转只在 iframe 中跳转,没有跳到父级。
需要在微信接口配置参数中设置 self_redirect:false
事件
IE
iframe?.contentDocument?.addEventListener('readystatechange', iframeHandleListener, {passive: false});
eq:
//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.");
};
}
非 IE
iframe?.contentDocument?.addEventListener('load', iframeHandleListener, {passive: false});
load
inline javascript
<script>
const onHandleLoad = () => console.log('iframe is loaded');
</script>
<iframe id="iframe" src="https://kangkk.com" onload="onHandleLoad(this)"></iframe>
javascript
<script>
document.getElementById('iframe').onload = () => console.log('iframe is loaded');
</script>
<iframe id="iframe" src="https://kangkk.com"></iframe>
addEventListener
<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>
Observables
<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>
No categories