How to make an iframe, where I can set the source code, not just the src? Is it possible to create one without the src?
-
For debugging purposes in Firefox & Chrome, you can use `var iframe=document.createElement('iframe');iframe.src="view-source:" + location.href;` to create an IFrame object. – Rob W Mar 12 '12 at 22:59
-
What are you trying to do? The point of an iframe is to show another page. If you don't want to show another page, but just write more html, than why don't you just use a div? – bhamlin Mar 12 '12 at 22:59
-
Don't understand what you're asking. The `src` attribute of an iframe specifies where to load the "source code" (really the source markup - html isn't "code") Are you saying you don't want to specify a 'src'? – Stephen P Mar 12 '12 at 23:00
-
If I use div, then the in mobile browsers no scrollbar appears – errgod Mar 12 '12 at 23:01
-
1Even if you set overflow: scroll? or overflow-x: scroll; overflow-y: scroll; in your div's style attribute? It sounds like atag is exactly what you're looking for.– Joshua Hayworth Mar 12 '12 at 23:05
-
IFrames will not solve your mobile scrollbar woe, and is a horrible, horrible solution. If I had to maintain your code, I would despair. Look into things like iScroll [ http://cubiq.org/iscroll-4 ] instead. – danp Mar 12 '12 at 23:19
2 Answers
//get the iframe
var iFrame = document.getElementById('iframe_id');
//this is your reference variable for the iframe body tag
var iFrameBody;
//get the body
if (iFrame.contentDocument ){// FF
iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
}
else if ( iFrame.contentWindow ){ // IE
iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
}
else {
iFrameBody = iFrame.contentDocument.body
}
iFrameBody.innerHTML = "i should be in the iframe";
references:
do note that this is bound to the "same-origin policy"
Also, you can actually write dynamically into the document that you want, like this:
<html>
<body>
<script type="text/javascript">
var wnd = window.open();
wnd.document.open("text/html");
wnd.document.write("<h1>Hello World!</h1>");
wnd.document.close();
</script>
</body>
</html>
This code opens an output stream (in a new window; about:blank), write some text, then close the output stream.
The open() method opens an output stream to collect the output from any document.write() or document.writeln() methods.
Once all the writes are performed, the document.close() method causes any output written to the output stream to be displayed.
Notes: If a document already exists in the target, it will be cleared. If document.open() isn't called to start writing, every call to write/writeLn will render the content immediately. So for performance reasons it's good to call open and close, in order to begin and end writing. Supported by all major browsers.
References: W3C: document.open

- 1,765
- 1
- 15
- 22