开发者

Can one set an iframe's src and manipulate its contents afterwards?

开发者 https://www.devze.com 2023-02-14 10:25 出处:网络
My goal is to have a parent page change the src of an iframe from blank to its proper url (as to utilize an onload handler in the iframe at a given point, but that\'s beside the point) and then manipu

My goal is to have a parent page change the src of an iframe from blank to its proper url (as to utilize an onload handler in the iframe at a given point, but that's beside the point) and then manipulate the iframe's contents. However, javascript seems oblivious to any elements of an iframe that aren't on its src when the DOM loads. Is there any way around this?

The setTimeouts are intended to allow the DOM and iframe to load. edit:fixed some stuff. Here's the containing page:

<html><head>
    <script type="text/javascript">
        var done = false;
        var theIframe;
        window.onload = function () {
            setTimeout('stuff()', 2000);
            clearTimeout('stuff()');
        }

        function stuff() {
            if (!done) {
                theIframe = window.myiframe;
                theIframe.src = 'http://localhost/TestStuff/redirectIframe.jsp';
                done = true;
                stuff();
            } else {
                theIframe.setMe = true;
            }
        }

      </script>
</head>
      <body>
      <iframe src="" width="500" height="500" id="myiframe" name="myiframe">

      </iframe>
     开发者_开发百科 </body>

And here's the iframe:

<html>
  <head>
    <script type="text/javascript">
        var setMe = false;
        window.onload = setInterval('checker()', 1000);
        function checker() {
            alert('hi');
            if (setMe) {
                window.onload = null;
                top.location = 'http://www.google.com';
                alert('foundit');
            }  else alert('a');

        }

    </script>
    </head>
  <body></body>
</html>

Any ideas?


In this piece of code:

theIframe.src = ...;
stuff();

you're calling stuff() immediately, contrary to what you have described, so in fact you're not allowing any time for the page to load. Maybe you're confused about how setTimeout works: it just schedules a single execution after the specified time, it doesn't automatically delay all calls to a function.

Also, you can use clearTimeout only with a previous ID returned by setTimeout, not with code as you have now.

Try something like this:

window.onload = function() {
  loadFrame();
}

function loadFrame() {
  theIframe = ...;
  theIframe.src = ...;
  setTimeout(setSomething, 2000);
}

function setSomething() {
  theIframe.setMe = true;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消