HTML 線上編輯器常見的嵌入方式有兩種:使用 object 或 iframe。兩者都需要透過表單與隱藏欄位,把編輯器內容送到後端。
在需要的位置加入 object 標籤,data 指向編輯器頁面,id 用於後續取值。
<object id="doc_html" data="gledit.htm" width="530" height="320" type="text/x-scriptlet" VIEWASTEXT></object>提交前,將編輯器內容複製到隱藏欄位。
<form method="post" action="add_news_save.asp" onsubmit="CheckForm()" name="form1">
<object id="doc_html" name="doc_html" data="gledit.htm" width="530" height="320" type="text/x-scriptlet" VIEWASTEXT></object>
<input type="hidden" name="content" />
</form>
<script language="javascript">
function CheckForm() {
document.form1.content.value = document.form1.doc_html.value;
}
</script>點擊插入圖片時開啟上傳視窗,上傳成功後回寫圖片路徑,並將 img 標籤加入編輯器。
var src = "<%= "upload/" & newname %>";
opener.form1.doc_html.value += "<img border=0 src=" + src + ">";
window.close();可將資料庫內容放入隱藏的 textarea,頁面載入時再複製到編輯器。建議使用 textarea 而非 hidden input,因為內容可能包含換行。
<body onload="document.form1.doc_html.value = document.form1.content.value">iframe 方式同樣需要隱藏欄位。提交時呼叫 iframe 頁面的 getHTML 方法。
<iframe src="gledit.htm" id="content_html" width="100%" height="100%"></iframe>
<script>
function subchk() {
document.form1.content.value = window.content_html.getHTML();
}
</script>若要插入圖片,可在 iframe 內的編輯區呼叫 pasteHTML,將 HTML 代碼插入目前游標位置。整體概念是:編輯器負責編輯,表單負責傳值,後端只要接收隱藏欄位即可。