5

I write jQuery script inside my iframe to hide h1 inside the iframe

<iframe id = "preview" width="800" height="500">   
</iframe>

 <script>
var H = "<html><head><script>$(\'#hh\').toggle();<";
H+= "/script></head><body><h1 id='hh'>JavaScript</h1></body></html>";

 var previewFrame = document.getElementById("preview");
 var preview =  previewFrame.contentDocument ||  previewFrame.contentWindow.document;
 preview.open();
 preview.write(H);
 preview.close();
  </script>

but,the script can't see the elements inside the iframe. when I moved the element with id='hh' out of the iframe, the script works.

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Sultan
  • 473
  • 5
  • 19

1 Answers1

4

It looks like you're trying to use jQuery inside the iframe for the script you're putting in the iframe, but you don't have jQuery in the iframe. The iframe is a whole different execution context. If you want to use jQuery in there, you have to load jQuery in there.

Also, you can't run a script that tries to modify the DOM from the <head> element. The DOM isn't loaded yet. You have to either move that script after the content in the <body> (e.g. right before </body>) or use $(document).ready(...) to run the script AFTER the DOM is ready.

OK, this is the sum of issues I found:

  1. You need jQuery in the iframe if you're going to use it there.
  2. You must use a separate <script> tag for loading jQuery. You can't use both a .src and a inline script in the same tag.
  3. You must use the proper $(document).ready(function() { your code here}) syntax.
  4. You must break apart the <script> and </script> that is in your JS string so the browser doesn't interpret it like '<scr' + 'ipt>'.

This works for me:

var H = '<html><body><h1 id="hh">JavaScript</h1>';
H += '<scr' + 'ipt src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></scr' + 'ipt>';
H += '<scr' + 'ipt>$(document).ready(function() {';
H += '$("#hh").hide();';
H += '});</scr' + 'ipt>';
H += '</body></html>';

var previewFrame = document.getElementById("preview");
var preview =  previewFrame.contentDocument ||  previewFrame.contentWindow.document;
preview.open();
preview.write(H);
preview.close();

I've broken up your H variable to make it more readable and easier to spot errors.

Working demo: http://jsfiddle.net/jfriend00/8L3KT/


FYI, this is the painful way to do this. Creating a full HTML document in JS is easy to make mistakes with.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I tried but it doesn't works,in addition it works when the script hide a h1 outside the iframe – Muhammad Sultan Jun 04 '14 at 17:58
  • i did load it and the same thing happens – Muhammad Sultan Jun 04 '14 at 18:02
  • @MuhammadSultan - see the additional issue I added to my answer. – jfriend00 Jun 04 '14 at 18:02
  • I did this: var H = "

    JavaScript

    – Muhammad Sultan Jun 04 '14 at 18:16
  • @MuhammadSultan - your script tags are messed up. The ` – jfriend00 Jun 04 '14 at 18:24
  • @MuhammadSultan - and it's `$(document).ready(function() {your code here})`, not what you have. – jfriend00 Jun 04 '14 at 18:26
  • @MuhammadSultan - Working code and demo added to my answer. Also have to break up your ` – jfriend00 Jun 04 '14 at 18:35
  • thanks very much,it did work,but what is the non painful way to do it please? – Muhammad Sultan Jun 04 '14 at 18:44
  • @MuhammadSultan - Much less painful to just put the content and scripts in the iframe as another other HTML document. Or put it in another HTML document and reference it with `src=xxx.html` in the iframe tag. Or just dynamically add content to the exist iframe document without writing a whole new one. – jfriend00 Jun 04 '14 at 18:49