简体   繁体   中英

dynamic IFRAME content - how to get the access to that IFRAME?

I'm loading a content dynamically to the <iframe>

<iframe style='border:none;' id='abc' src="http://localhost:39217/Home/GetContent/some_dynamic_code"></iframe>

after a success response, in that iframe is that content

<html>
   <head>
      <script type="text/javascript">
          function onPageLoad() {
              if (document.readyState === "complete") {
                  var cont = document.getElementById("abc");
                  alert(cont);    
              }
          }
      </script>
   </head>

   <body onload='onPageLoad()'>
      <a target="_blank" href='http://lorem'>
        <img class='abc' style='max-width:300px; max-height: 38px;' alt='' src='/Images/image.png' />
      </a>
   </body>
</html>

That iframe will be using outside my site (by users), but I want to have the ability to change the <img> src . But, I need also to change the width/height of the iframe after I change the image. So, how can I get the access to that iframe using JS ? That code above alerts me null

I made an example for you here: http://jsfiddle.net/KRaWU/2/

I use jQuery to achieve that and I suggest you do the same.

// this will find a button within an iframe
var obj = $('iframe').contents().find('.actionButton').find('input[type="submit"]');
// this will change the value of the button, and you can see that the text is changed.
obj.attr('value', 'LOG ME IN');

You can analogically find an img and change its src.

JS interaction between iFrames and their parents is for what I know impossible or at least troublesome. I know there is somewhere a property window.frames and maybe even frame.parent but in general, JS interaction like that is impossible. I think you should consider another type of solution (like an ajaxcall maybe, if that could satisfy your needs).

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