0

I am coding some kind of display made of an iframe. When I click a button, which is outside the iframe, it should right something down on the iframe. Is it possible?

<iframe src = "display.html" id = "ifr"></iframe>

    <div class = "buttons" onClick = "showSequence()">
        Get the whole sequence
    </div>

The javascript code:

function showSequence(){ var iframe = document.getElementById("ifr"); (iframe.contentDocument || iframe.contentWindow.document).write("Something!"); }

1 Answers1

2

Yes, if the iframe is in the same domain. Here is a code example:

<button onclick="test()">Click to write in iframe</button>

<iframe id='ifr'></iframe>
<script>
    function test(){
        var iframe = document.getElementById("ifr"); 
        //contentDocument because IE8 doesn't support contentWindow
        (iframe.contentDocument || iframe.contentWindow.document).write("hello iframe");   
    }
</script>
fmodos
  • 4,472
  • 1
  • 16
  • 17
  • Don't forget to include support for browsers that use `.contentDocument` instead of `.contentWindow.document` – Ian May 16 '13 at 02:56
  • @Ian this is new information for me... do you have any reference about the browsers that support contentDocument? – fmodos May 16 '13 at 02:58
  • 1
    http://stackoverflow.com/questions/6581803/contentdocument-for-an-iframe . I would use `var iframe = document.getElementById("ifr"); (iframe.contentDocument || iframe.contentWindow.document).write("hello iframe");` – Ian May 16 '13 at 03:00
  • That makes a lot of sense, but I can't make it work here :/ Take a look at the code: function showSequence(){ document.getElementById('pageDisplay').contentWindow.document.write("Something!"); } – Renato Bispo May 16 '13 at 03:05
  • @RenatoBispo Without any of your code, there's no way to help. fmodos provided a way to do it that should work. And you never answered whether the pages had the same domain or not – Ian May 16 '13 at 03:07
  • @RenatoBispo your code looks fine... what browser are you using? does it show any error? ps: Ian just updated my answer, thanks for the information – fmodos May 16 '13 at 03:09
  • I am using Google Chrome. No, no, it doesn't show any error message. :/ Can't find out why it's not working. – Renato Bispo May 16 '13 at 03:16
  • Ok, I tested your code and it worked for me... did you put the javascript code inside the – fmodos May 16 '13 at 03:23
  • It's an external file. Is that the problem? I know it's well 'linked' because I tested some stuff on it. – Renato Bispo May 16 '13 at 03:26
  • no, it shouldn't be... please check the javascript console on chrome and make sure that there is no error. – fmodos May 16 '13 at 03:29
  • Uncaught TypeError: Cannot call method 'write' of undefined. That's what I got, after that last line. But man, don't worry, you have already helped me a lot, thank you very much. I will keep looking for a solution and then, if I get something, I will post it here later ;D. Thank you all. See you. – Renato Bispo May 16 '13 at 03:55
  • Also got this: Unsafe JavaScript attempt to access frame with URL file:///C:/Users/myUser/Desktop/display.html from frame with URL file:///C:/Users/myUser/Desktop/index.html. Domains, protocols and ports must match. – Renato Bispo May 16 '13 at 03:59
  • oh this happens when opening the html by the physical location, dont know why... but it will work if you put it in a http server like apache and access by an url ie: http://localhost/yourfile.html, I suggest you to create a new post with this question maybe someone else can help you. – fmodos May 16 '13 at 04:06
  • Oh fmodos, I think I got it. I tested your code in a new HTML document, and I noticed that there is no "src" attribute in your iframe tag. But there is one in mine. I deleted this "src" atributte and it worked! Don't know why exactly... – Renato Bispo May 16 '13 at 16:42