HTML 線上編輯器 (如舊版 eWebEditor、FCKeditor、KindEditor 等) 在經典 ASP/ASP.NET/PHP 專案中廣泛使用。本文系統整理兩種主流嵌入模式 (`
<object> 嵌入 (IE 專用、傳統做法)<form method="post" action="article_save.asp" name="form1" onsubmit="return syncEditor()">
<object id="doc_html" name="doc_html"
data="editor.htm"
width="100%" height="400"
type="text/x-scriptlet"
VIEWASTEXT></object>
<input type="hidden" name="content" id="hiddenContent" />
<input type="submit" value="儲存" />
</form>
<script language="javascript">
function syncEditor() {
// 關鍵:編輯器物件需暴露 .value 或 .getContent() 方法
// 不同編輯器 API 不同,常見寫法:
try {
document.form1.content.value = document.form1.doc_html.value; // 若編輯器支援 .value
} catch(e) {
try {
document.form1.content.value = document.form1.doc_html.getContent(); // 如 eWebEditor
} catch(e2) {
alert("無法取得編輯器內容:" + e2.message);
return false;
}
}
return true; // 允許表單提交
}
</script>
<body onload="loadEditorContent()">
<script>
function loadEditorContent() {
// 從隱藏 textarea 讀取資料庫內容 (伺服器端已渲染)
var dbContent = document.form1.content.value;
try {
document.form1.doc_html.value = dbContent;
} catch(e) {
try {
document.form1.doc_html.setContent(dbContent); // 如 eWebEditor
} catch(e2) {
console.error("編輯器回填失敗", e2);
}
}
}
</script>
<iframe> 嵌入 (跨瀏覽器、現代標準)<form method="post" action="article_save.asp" name="form1" onsubmit="return syncEditorIframe()">
<iframe id="editorFrame" name="editorFrame"
src="editor.htm"
style="width:100%; height:400px; border:1px solid #ccc;"></iframe>
<input type="hidden" name="content" id="hiddenContent" />
<input type="submit" value="儲存" />
</form>
<script>
function syncEditorIframe() {
var editorWin = window.frames["editorFrame"]; // 或 document.getElementById("editorFrame").contentWindow
if (editorWin && editorWin.getHTML) {
document.form1.content.value = editorWin.getHTML();
return true;
}
alert("編輯器未就緒或不支援 getHTML()");
return false;
}
function loadEditorContentIframe() {
var editorWin = window.frames["editorFrame"];
var dbContent = document.form1.content.value; // 伺服器端渲染的初始內容
if (editorWin && editorWin.setHTML) {
editorWin.setHTML(dbContent);
}
}
</script>
<body onload="loadEditorContentIframe()">
<%@ Language=VBScript %>
<%
' image_upload.asp - 處理上傳並回傳 JS
' ... 上傳邏輯 (略) ...
Dim newFilePath : newFilePath = "/uploads/images/" & newFileName
%>
<script language="javascript">
var src = '<%= newFilePath %>';
// 向父頁面編輯器插入圖片標籤
if (window.opener && window.opener.insertImage) {
window.opener.insertImage(src); // 編輯器需提供 insertImage(url) 方法
} else if (window.parent && window.parent.editorFrame) {
window.parent.editorFrame.contentWindow.insertImage(src); // iframe 模式
}
window.close();
</script>
編輯器需實作 `insertImage(url)` 方法,內部執行 `editor.pasteHTML('')` 或相應 API。
<!-- article_save.asp -->
<%
Dim content : content = Request.Form("content")
' 1. 基礎 XSS 過濾 (建議用成熟庫 AntiXSS / HTML Purifier)
content = Replace(content, "
| 編輯器 | 授權 | 相容性 | 特色 |
|---|---|---|---|
| TinyMCE | LGPL/商業 | 全瀏覽器 | 功能最全、插件豐富、CDN 部署簡單 |
| CKEditor 4/5 | GPL/商業 | 全瀏覽器 | 企業級、Accessibility 佳、React/Vue 整合包 |
| Quill | BSD | 現代瀏覽器 | 輕量、模組化、Delta 格式資料模型 |
| Editor.js | MIT | 現代瀏覽器 | 區塊式、輸出 JSON、適合結構化內容 |
| wangEditor | MIT | 全瀏覽器 | 中文文檔完善、輕量、支援 Vue/React |
避坑指南:舊專案維護請繼續用原編輯器;新專務**直接選現代編輯器 + 前後端分離架構**,勿再用 `
無論 `