需要以 POST 方式傳送資料並跳轉頁面,又不想額外引入框架時,可以動態建立一個隱藏表單再送出,這是相當乾淨的解法。
function post(URL, PARAMS) {
var temp = document.createElement("form");
temp.action = URL;
temp.method = "post";
temp.style.display = "none";
for (var x in PARAMS) {
var opt = document.createElement("textarea");
opt.name = x;
opt.value = PARAMS[x];
temp.appendChild(opt);
}
document.body.appendChild(temp);
temp.submit();
}
post('pages/statisticsJsp/excel.action', {
html: prnhtml,
cm1: 'sdsddsd',
cm2: 'haha'
});
textarea 建立,好處是能容納大量文字與換行,作為匯出報表、傳送 HTML 片段時特別方便;若資料量小,改用 input 也可以。submit(),部分舊版 IE 若跳過這步會送出失敗。