1

I am trying to change the source of the iframe to javascript:... I tried this without any luck;

<iframe id="iframed" src="http://example.com"></iframe>

$('#iframed').attr('src','javascript:document.write("hello world")');

I am getting "TypeError: document.write is not a function" error. What is the correct way to do that ?

user198989
  • 4,092
  • 17
  • 57
  • 85
  • 1
    Are you trying to put content in the ` – Pointy Dec 03 '14 at 18:53
  • 1
    That error sounds unrelated. – isherwood Dec 03 '14 at 18:54
  • If you write javascript:document.write("hello world") to url bar, it works. So why we cannot do that on the iframe. @pointy, yes. – user198989 Dec 03 '14 at 18:56
  • 2
    If the iframe is the same origin as the document your code is in, you can just directly change the contents of the iframe. If it is not, then you may as well just replace the iframe with a different iframe that has the content in it you want. – jfriend00 Dec 03 '14 at 18:57

4 Answers4

3

If you just want to write content to the frame, you can get direct access to its document object:

document.getElementById("iframed").contentDocument.write("Hello World!");

This won't work if the frame is already loaded from a foreign domain however.

Pointy
  • 371,531
  • 55
  • 528
  • 584
  • It doesn't work in any of Internet Explorer versions. It can't read the contentDocument.write. Does that function has different name in ie ? – user198989 Dec 03 '14 at 19:05
  • @user198989 in old versions of IE, you'd access `contentWindow.document`, but I think it should work like other browsers in versions from IE8 on. – Pointy Dec 03 '14 at 19:07
  • I understand. Possible to put instead of Hello world ? – user198989 Dec 03 '14 at 19:08
  • @user198989 you can write whatever you want. You'd probably get more useful information if you'd expand your question and explain what your overall goals are. *Why* do you want to create content in the frame? – Pointy Dec 03 '14 at 19:11
3

There is no standard I know of which defines what should happen when you set the src attribute on an iframe to a URI with the javascript: scheme. There is a draft IETF note which suggests what functionality already exists (like its use in anchor elements), but does not say user agents have to do anything. The fact that you can enter it into the address bar of a browser is due to the browser's implementation. For instance, I'm guessing entering that URL into a command-line browser like lynx would get you nowhere.

If you want to communicate between frames using JavaScript, that would be a different question.

Heretic Monkey
  • 10,498
  • 6
  • 45
  • 102
0
<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <iframe id="iframed"></iframe>
        <script>
         var test = "Hello World!";
         document.getElementById("iframed").src = "data:text/html;charset=utf-8," + test;
        </script>
    </body>
</html>

Try this it works in chrome...also check out this link

Community
  • 1
  • 1
brso05
  • 12,634
  • 1
  • 17
  • 37
0

Since you're trying to put content additional content inside the iframe, you first need to know that it's likely to be impossible if you have no control over the site inside the frame (in your example "http://exemple.com").

Else it would be easy to inject scripts, which would cause various security issues.

If you have control over the site inside the iframe, you may make it able to understand messages originating from the site including the iframe. If you want to do this way, you may want to look at "How to communicate between iframe and the parent site?".

Community
  • 1
  • 1
dotpush
  • 420
  • 3
  • 14