简体   繁体   中英

How to call javascript after frame is fully rendered?

I am looking for a way to modify an iFrame attribute (a textbox value actually). The iFrame is on another domain and I do have access to it. From what I understand I must do that after the entire page ( iFrame included) is rendered. I tryed something like that:

<script type="text/javascript">
$(window).load(function() {
    alert("test");
    d = document.getElementById("myframe");
    d.contentWindow.document.getElementById("frametxtbox");
});
</script>

But I always get the following error:

Cannot call method 'getElementById' of undefined

I also noticed that the alert pops up BEFORE the iFrame is rendered, so I think my script is executed before page has access to frame values and, therefore I get the error. I have very few knowledge of web-related programming (always worked on backend side) so forgive me if my question maybe makes no sense.

If the iframe points to another domain, it'll be subject to same origin policy . This means that before you try to access its document you'll have to check the wiki and relax the policy (check the link for details). After that you'll need to bind ready event to the iframes document, rather than your main page doc.

$(window.frames['myframe'].document).ready(function() { alert('moo'); });

See if this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM