zoukankan      html  css  js  c++  java
  • How to get the height of a crossdomain iframe page All In One

    How to get the height of a cross-domain iframe page All In One

    如何获取跨域 iframe 页面高度 All In One

    1. postMessage
    // iframe page
    function debounce (func, times = 1000) {
        let timer = null;
        return function () {
            if (timer){
                clearTimeout(timer);
            }
            timer = setTimeout(func, times);
        };
    };
    
    const sendScrollMessage = () => {
        const app = document.querySelector('#app');
        const height = Math.abs(app.scrollTop);
        parent.postMessage({
            type: 'iframeScroll',
            height: height,
        }, 'https://iframe.xgqfrms.xyz/parent');
    };
    
    const app = document.querySelector('#app');
    app.addEventListener('scroll', (event) => {
        debounce(sendScrollMessage, 100)();
    }, false);
    
    
    // parent page
    const iframe = document.querySelector(`[id="iframe"]`);
    // get iframe height ??? postMessage
    console.log('iframe', iframe);
    iframe.onload = function() {
        let count = 0;
        let timer = setInterval(() => {
            count++;
            if(count > 100) {
                clearInterval(timer);
            }
            console.log('send message to iframe');
            iframe.contentWindow.postMessage({
                type: "iframeScrollEvent",
                other: "other data to pass",
                height: 100,
            }, "*");
        }, 1000);
        console.log('timer', timer);
    };
    
    

    live demo

    refs



    ©xgqfrms 2012-2020

    www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

    原创文章,版权所有©️xgqfrms, 禁止转载 ️,侵权必究⚠️!


  • 相关阅读:
    配置centOS下的Python
    linux基础命令2
    linux基础命令1
    linux常用命令(运维用到)
    Lab 10-2
    Lab 10-1
    Lab 9-3
    archlinux 装完系统连接 wifi 网络
    arch Linux(二)
    arch Linux 安装完,无法通过 SSH 远程连接 root 用户问题
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/15687190.html
Copyright © 2011-2022 走看看