---
title: "在线编辑器 兼容Firefox,IE Demo"
author: "hwpok"
date: "2008-12-05T14:30:00Z"
tags: []
slug: "firefox-ie-demo"
draft: false
lastmod: "2008-12-05T14:30:00Z"
id: 615786
url: "https://hwpok.iteye.com/blog/615786"
---

[rw\_editor.js]()

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;/*  
\*write by 惠万鹏  
\*date 2008-12-04  
\*/

/\** 判断是ie浏览器还是火狐浏览器 \*/  
var IE\_BROWSER = 0;  
var FF\_BROSWER = 1;

var browserType = IE\_BROWSER;  
if (window.netscape)  
{  
    browserType = FF\_BROSWER;  
} 

/\** 得到iframe对象的内容窗口 \*/  
var oEditor = document.getElementById("editeFrame").contentWindow;

oEditor.document.designMode = 'On';  
oEditor.document.contentEditable = true;  
/\** 兼容火狐浏览器 \*/  
oEditor.document.write('&lt;html&gt;&lt;body&gt;aaa&lt;/body&gt;&lt;/html&gt;');  
oEditor.document.close();

/\** 个性化窗口 \*/  
var individuationWin = null;  
/\** 更改代码窗口 \*/  
var editorWin = null;      
/\** 预览窗口 \*/  
var previewWin = null;  
/\** 编辑开始\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/  
function initialHtml(hmtl)  
{  
    oEditor.document.designMode = "On";  
    oEditor.document.contentEditable = true;  
    oEditor.document.write(hmtl);  
    oEditor.document.close();  
}    
/\** 编辑结束\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/

/\** 编辑开始\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/  
function editor()  
{  
    if(editorWin == null  || editorWin.closed == true)  
    {  
        var iWidth = 800,iHeight = 600; //弹出窗口的宽度,弹出窗口的高度;  
         var iTop = (window.screen.availHeight-30-iHeight)/2; //获得窗口的垂直位置;  
          var iLeft = (window.screen.availWidth-10-iWidth)/2;  //获得窗口的水平位置;  
            
          var winParameters = "width=" + iWidth + ",height=" + iHeight;  
          winParameters += ",left=" + iLeft + ",top=" + iTop;  
          winParameters += ",menubar=no,scrollbars=yes, resizable=yes,location=no, status=no";  
      
        editorWin = window.open("editCode.html","编辑原代码窗口",winParameters);  
    }  
    else  
    {  
        editorWin.focus();  
    }  
}  
/\** 编辑结束\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/

/\** 个性化开始\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/  
function individuation()  
{  
    /\** 兼容ie和fireFox \*/  
    if(individuationWin == null || individuationWin.closed == true)  
    {  
        var iWidth = 200,iHeight = 100;  
         var iTop = (window.screen.availHeight-30-iHeight)/2;  
          var iLeft = (window.screen.availWidth-10-iWidth)/2;  
            
          var winParameters = "width=" + iWidth + ",height=" + iHeight;  
          winParameters += ",left=" + iLeft + ",top=" + iTop;  
          winParameters += ",menubar=no,scrollbars=yes, resizable=yes,location=no, status=no";  
            
        individuationWin = window.open("individuation.html","个性化窗口",winParameters);  
    }  
    else  
    {  
        individuationWin.focus();  
    }  
}  
/\** 个性化结束\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/

/\** 预览开始\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/  
function perview()  
{  
    if(previewWin != null && !previewWin.closed)  
    {  
        previewWin.close();  
    }  
    var iWidth = 1024, iHeight = 800;  
     var iTop = (window.screen.availHeight-30-iHeight)/2;  
      var iLeft = (window.screen.availWidth-10-iWidth)/2;  
        
      var winParameters = "width=" + iWidth + ",height=" + iHeight;  
      winParameters += ",left=" + iLeft + ",top=" + iTop;  
      winParameters += ",menubar=no,scrollbars=yes, resizable=yes,location=no, status=no";  
      
    previewWin = window.open("","预览",winParameters);  
    var html = getAllText();  
    previewWin.document.write(html);  
}  
/\** 预览结束\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/

/\** 得到所有文本-开始\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/  
function getAllText()  
{  
    var html = "html&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;";  
    if(browserType == IE\_BROWSER)  
    {  
        html = \_getAllText4IE();  
    }  
    else if(browserType == FF\_BROSWER)  
    {  
        html = \_getAllText4FF();  
    }  
    else  
    {  
        alert("this software only for ie and firefox!");  
    }  
    return html;  
}

function \_getAllText4IE()  
{  
    return oEditor.document.lastChild.outerHTML;  
}

function \_getAllText4FF()  
{  
    /\** fireFox下没有outerHTML,所以手工加上html标签 \*/  
    return "&lt;html&gt;" + oEditor.document.documentElement.innerHTML + "&lt;/hmtl&gt;";  
}  
/\** 得到所有文本-结束\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** \*/

/\*\*复制-开始 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
function copy()  
{  
    if(browserType == IE\_BROWSER)  
    {  
        \_getCopy4IE();  
    }  
    else if(browserType == FF\_BROSWER)  
    {  
        \_getCopy4FF();  
    }  
    else  
    {  
        alert("this software only for ie and firefox!");  
    }  
}

function \_getCopy4IE()  
{  
    var selectedText = oEditor.document.selection.createRange().text;  
    /\** 执行拷贝操作 \*/  
    setClipboard(selectedText);   
}

function \_getCopy4FF()  
{  
    /\** 火狐下得到选中的文本 \*/  
    var selectedText = oEditor.getSelection().toString();  
    /\** 执行拷贝操作 \*/  
    setClipboard(selectedText);   
}  
/\*\*复制-结束 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

/\*\*粘贴-开始\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
function paste()  
{  
    var html = getClipboard();  
    insertHTML(html);  
}  
/\*\*粘贴-结束\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

/\*\*插入HTML-开始\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/    
function insertHTML(html)  
{  
    if(browserType == IE\_BROWSER)  
    {  
        \_insertHTML2IE(html);  
    }  
    else if(browserType == FF\_BROSWER)  
    {  
        \_insertHTML2FF(html);  
    }  
    else  
    {  
        alert("this software only for ie and firefox!");  
    }  
}

function \_insertHTML2IE(html)  
{  
    if (oEditor.document.selection.type.toLowerCase() != "none")  
    {  
        oEditor.document.selection.clear() ;  
    }  
    oEditor.document.selection.createRange().pasteHTML(html) ;   
}

function \_insertHTML2FF(html)  
{  
    var r = oEditor.getSelection().getRangeAt(0);  
    var oFragment = r.createContextualFragment(html);   
    r.deleteContents();  
    r.insertNode(oFragment);  
}  
/\*\*插入HTML-结束\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/

editor.html

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;  
&lt;html&gt;  
&lt;head&gt;  
    &lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt;  
&lt;/head&gt;  
&lt;body&gt;  
    &lt;input type="button" value="复制" onclick="copy();"  
    /&gt;&lt;input type="button" value="粘贴" onclick="paste();"  
    /&gt;&lt;input type="button" value="编辑" onclick="editor();"  
    /&gt;&lt;input type="button" value="个性" onclick="individuation();"  
    /&gt;&lt;input type="button" value="预览" onclick="perview();"/&gt;&lt;br /&gt;  
    &lt;iframe id="editeFrame" marginheight="1" marginwidth="1" width="550" height="286" scrolling="auto"&gt;&lt;/iframe&gt;  
    &lt;script type="text/javascript" src="copyPaste.js"&gt;&lt;/script&gt;  
    &lt;script type="text/javascript" src="rw\_editor.js"&gt;&lt;/script&gt;  
&lt;/body&gt;  
&lt;/html&gt;

[individuation.html]()

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;  
&lt;html&gt;  
  &lt;head&gt;  
    &lt;title&gt;个性化设置&lt;/title&gt;  
    &lt;script type="text/javascript"&gt;  
        function individuation()  
        {  
            window.opener.insertHTML(document.getElementById("individuationType").value);  
        }  
    &lt;/script&gt;  
  &lt;/head&gt;  
    
  &lt;body&gt;  
    &lt;select id="individuationType" name="individuationType" style="width:100;"&gt;  
        &lt;option value="#name#"&gt;姓名&lt;/option&gt;  
        &lt;option value="#sex#"&gt;性别&lt;/option&gt;  
        &lt;option value="#age#"&gt;年龄&lt;/option&gt;  
        &lt;option value="#email#"&gt;EMAIL&lt;/option&gt;  
        &lt;option value="#address#"&gt;地址&lt;/option&gt;  
    &lt;/select&gt;  
    &lt;input type="button" value="确定" onclick="individuation();"&gt;  
  &lt;/body&gt;  
&lt;/html&gt;