[Learning] method calls the iframe

When pages have iframe, think of ways to call an iframe on the main page, you can use contentWindow property. But there is one thing to pay attention to the specific use, such as page load is to be completed before they can, otherwise they will not find the error function.

Example:

Parent page:

<iframe id="son" src="a.html"></iframe>

 

Subpage:

<body>
    This is a sub-page
    <script>
        function test() {
            console.log ( " method subpage " );
        }
    </script>
</body>

 

If the direct write:

document.getElementById('son').contentWindow.test();

 

 

modify:

window.onload=function(){
    document.getElementById('son').contentWindow.test();
}

or:

var map_iframe = document.getElementById("son");
map_iframe.onload = function() {    
    map_iframe.contentWindow.test();
};

 

that's it:

 

Guess you like

Origin www.cnblogs.com/hzhjxx/p/11850814.html