Javascript 如何在跨域url中调整iframe的大小

Javascript 如何在跨域url中调整iframe的大小,javascript,angularjs,iframe,Javascript,Angularjs,Iframe,任何人都可以告诉我,我正在尝试在iframe onload函数中加载url。我想在没有滚动条的情况下显示iframe,并调整其内容,但父页面将根据内容进行滚动。有人能告诉我如何在跨域url中调整iframe的大小吗 谢谢为了根据iframe的内容调整iframe的大小,您可以使用将当前iframe高度发送给其父级 包含在iFrame中的页面需要使用postMessage将其当前高度发送到包含页面。 包含页面必须侦听该消息,并相应地更改iframe的高度 包含页面中的代码可能如下所示: funct

任何人都可以告诉我,我正在尝试在iframe onload函数中加载url。我想在没有滚动条的情况下显示iframe,并调整其内容,但父页面将根据内容进行滚动。有人能告诉我如何在跨域url中调整iframe的大小吗


谢谢

为了根据iframe的内容调整iframe的大小,您可以使用将当前iframe高度发送给其父级

包含在iFrame中的页面需要使用postMessage将其当前高度发送到包含页面。 包含页面必须侦听该消息,并相应地更改iframe的高度

包含页面中的代码可能如下所示:

function getBottom() {
    var max = jQuery("body").height();
    //Your additional calculations go here
    return max;
}

function updateHeight() {
    sendHeightToParent();
    setTimeout(updateHeight, 500);
}

function sendHeightToParent() {
    var messageObject = {
        "type":"size",
        "version":"1.0.0",
        "params":{
            "height": getBottom()
        }
    };

    //YOURTARGET is the target page if known. Otherwise use "*"
    parent.postMessage(JSON.stringify(messageObject), "YOURTARGET");
}

//Only possible if html5 post message is available
if (typeof parent.postMessage == 'function') {
    jQuery().ready(function() {
        sendHeightToParent();
        setTimeout(updateHeight, 500);
    });
}
然后,父级侦听事件并相应地更新iframe:

//Only possible if html5 post message is available
if (typeof parent.postMessage == 'function') {
    window.addEventListener("message", function (e) {
        obj = JSON.parse(e.data);
        if (obj.type == 'size') {
            myFrame = document.getElementById('YouIFrameId');
            myFrame.stye.height = obj.params.height + "px";
        }
    }
}

为了根据iframe的内容调整其大小,可以使用将当前iframe高度发送给其父级

包含在iFrame中的页面需要使用postMessage将其当前高度发送到包含页面。 包含页面必须侦听该消息,并相应地更改iframe的高度

包含页面中的代码可能如下所示:

function getBottom() {
    var max = jQuery("body").height();
    //Your additional calculations go here
    return max;
}

function updateHeight() {
    sendHeightToParent();
    setTimeout(updateHeight, 500);
}

function sendHeightToParent() {
    var messageObject = {
        "type":"size",
        "version":"1.0.0",
        "params":{
            "height": getBottom()
        }
    };

    //YOURTARGET is the target page if known. Otherwise use "*"
    parent.postMessage(JSON.stringify(messageObject), "YOURTARGET");
}

//Only possible if html5 post message is available
if (typeof parent.postMessage == 'function') {
    jQuery().ready(function() {
        sendHeightToParent();
        setTimeout(updateHeight, 500);
    });
}
然后,父级侦听事件并相应地更新iframe:

//Only possible if html5 post message is available
if (typeof parent.postMessage == 'function') {
    window.addEventListener("message", function (e) {
        obj = JSON.parse(e.data);
        if (obj.type == 'size') {
            myFrame = document.getElementById('YouIFrameId');
            myFrame.stye.height = obj.params.height + "px";
        }
    }
}

从body标签我需要调用哪个方法,或者需要从iframe调用哪个方法。您可以更新包含iframe的页面中的代码吗?您可以添加第二个代码。在包含的页面中添加第一个代码..我在index.jspfrom body标记的同一页面中添加了iframe,我需要调用或需要从iframe调用哪个方法。您能否更新包含添加第二个代码的iframe的页面中的代码。在包含的页面中添加第一个代码..我在index.jsp的同一页面中添加了iframe