pasteHTML() for Firefox

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) {

댓글을 남겨주세요

This site uses Akismet to reduce spam. Learn how your comment data is processed.