This article details how to create an iframe to use as a replacement for a <textarea>; the iframe is intended to be the destination for user input in a rich text control.
A working example can be seen here.
This works in newer versions of most browsers. The two exceptions I know of to date are Safari and Konqueror; support of the designMode property for these browsers is expected.
Before we begin we will create a simple HTML document; we can not use an XHTML DTD or our page will technically contain invalid markup:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>RTE - dynamic iframe</title>
<script type="text/javascript" src="rte.js"></script> <!-- rte.js IS THE FILE WE WILL BE CREATING -->
<style type="text/css">
h1 {
font-size: 1.0em;
}
</style>
</head>
<body>
<h1>Rich Text Editing - Dynamic iframe</h1>
</body>
</html>
The first thing we must do is create an onload event handler; we do this because we will be attaching the iframe to the body element but the body element won't be available until after the document is parsed.
// For simplicity's sake, we will do this the old fashioned way.
window.onload = function(){
}
window.onload = function(){
var iframe = document.createElement("iframe"); // #1
}
window.onload = function(){
var iframe = document.createElement("iframe"); // #1
document.getElementsByTagName("body")[0].appendChild(iframe); // #2
}
window.onload = function(){
var iframe = document.createElement("iframe"); // #1
document.getElementsByTagName("body")[0].appendChild(iframe); // #2
var w3c = iframe.contentDocument !== undefined ? true : false; // Shortcut
var idoc = w3c ? iframe.contentDocument // Shortcut
: iframe.contentWindow.document;
}
window.onload = function(){
var iframe = document.createElement("iframe"); // #1
document.getElementsByTagName("body")[0].appendChild(iframe); // #2
var w3c = iframe.contentDocument !== undefined ? true : false; // Shortcut
var idoc = w3c ? iframe.contentDocument // Shortcut
: iframe.contentWindow.document;
idoc.open(); // #3
idoc.close(); // #3
}
window.onload = function(){
var iframe = document.createElement("iframe"); // #1
document.getElementsByTagName("body")[0].appendChild(iframe); // #2
var w3c = iframe.contentDocument !== undefined ? true : false; // Shortcut
var idoc = w3c ? iframe.contentDocument // Shortcut
: iframe.contentWindow.document;
idoc.open(); // #3
idoc.close(); // #3
idoc.designMode = w3c ? "on" : "On"; // #4
}
window.onload = function(){
var iframe = document.createElement("iframe"); // #1
document.getElementsByTagName("body")[0].appendChild(iframe); // #2
var w3c = iframe.contentDocument !== undefined ? true : false; // Shortcut
var idoc = w3c ? iframe.contentDocument // Shortcut
: iframe.contentWindow.document;
setTimeout(function(){
var the_iframe = iframe;
if(the_iframe.contentDocument){
the_iframe.contentDocument.designMode = "on";
}else if(the_iframe.contentWindow){
the_iframe.contentWindow.document.designMode = "On";
}
}, 250);
}
Comments
Post new comment