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 ?

4
  • 1
    Are you trying to put content in the <iframe>? Commented Dec 3, 2014 at 18:53
  • 1
    That error sounds unrelated. Commented Dec 3, 2014 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. Commented Dec 3, 2014 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. Commented Dec 3, 2014 at 18:57

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 in old versions of IE, you'd access contentWindow.document, but I think it should work like other browsers in versions from IE8 on.
I understand. Possible to put <script>//some js </script> instead of Hello world ?
@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?
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.

Comments

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

6 Comments

document.write() was just an example. Could we run any other js functions with your method ?
I'm not sure what you mean?
Can I change the hello world text to javascript ? So that I can run javascript ?
@user198989 Why not just create a separate html page and link to that page in your iframe?
Doesnt work in Internet Explorer. The browser tries to open the page using some application. Very strange
|
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?".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.