IE has a handy function called pasteHTML while the other browsers implementing DOM2 Range don't.
var sel = document.selection;
if (sel) {
var rng = sel.createRange();
if (!rng) rng.pasteHTML("<strong>Hello, <em>World!</em></strong>");
}
The following code snippet shows how to implement the same function for Firefox, Google Chrome, Opera and Safari.
var sel = document.getSelection();
if (sel.rangeCount) {
var rng = sel.getRangeAt(0);
var frag =document.createDocumentFramgment();
var div = document.createElement("div");
div.innerHTML = "<strong>Hello, <em>World!</em></strong>";
while(div.firstChild) {
frag.appendChild(div.firstChild);
}
rng.deleteContents();
rng.insertNode(frag);
}