之前寫了一支OpenWindow2的JavaScript function,讓我們可以方便在開啟新視窗的時候透過POST傳值,避免參數在URL中因為長度太長被截斷的問題!
之後開發過程當中,遇到需要背景傳遞資料出去,但是不希望開啟新視窗,於是用OpenWindow2相同的傳值邏輯,開發一個是呼叫iframe的。
關鍵就在這幾行:
var iframe = null;
var iDoc = null;
iframe = $(str_name);
iDoc = iframe.contentDocument || iframe.contentWindow.document;
iDoc.write(arr_data.join(""));
str_name是 iframe 的 id 名稱,iDoc則是負責取得 iframe 的 document 物件,一但取得document物件,就可以任意 write 你想要的字串出去..^^..
資料來源參考自下列網址:
http://www.dotblogs.com.tw/puma/archive/2008/11/13/5982.aspx
(感謝Google大神讓我幾分鐘內就找到相關的範例參考)
※以下是原始碼:
1: function OpenFrame(str_url, str_name, dt_param, str_message)
2: {
3: var iframe = null;
4: var iDoc = null;
5: var str_form = "";
6: var str_form_head = "<form name=\"frm_param\" action=\"%url%\" method=\"post\">";
7: var str_form_foot = "</form>";
8: var str_form_param = "<textarea name=\"%param_name%\" cols=\"20\" rows=\"5\" wrap=\"off\" style=\"display:none\">%param_value%</textarea>";
9: var str_javascript_execute = "<script type=\"text/javascript\">document.frm_param.submit();</sc" + "ript>";
10: var arr_data = new Array();
11:
12: str_form_head= str_form_head.ReplaceAll("%url%", str_url);
13:
14: arr_data.push(str_message);
15: arr_data.push(str_form_head);
16:
17:
18: with (dt_param)
19: {
20: for (var col = 0; col < getFieldCount(); col++)
21: {
22: var str_param_name = getFieldName(col);
23: var str_param_value = getValue(str_param_name);
24: var str = str_form_param;
25:
26: str = str.ReplaceAll("%param_name%", str_param_name);
27: str = str.ReplaceAll("%param_value%", str_param_value);
28:
29: arr_data.push(str);
30: }
31: }
32: arr_data.push(str_form_foot);
33: arr_data.push(str_javascript_execute);
34:
35: iframe = $(str_name);
36: iDoc = iframe.contentDocument || iframe.contentWindow.document;
37: iDoc.write(arr_data.join(""));
38:
39: return (iDoc);
40: }
全站熱搜
留言列表