function printNotice(noticeId,template){if (noticeId == '' || noticeId =='null') {alert('請儲存後重試。');}else{ var noticeOrgName='${taskNoticesDTO.agency}';var noticeOrgNo='${taskNoticesDTO.taskNo}';var noticeContent=document.getElementById("note").innerHTML.replace(/[\r\n]/g,"<br>");var noticeDate='<s:date name="taskNoticesDTO.decideDate" format="yyyy-MM-dd"/>';alert(noticeContent); var url='${pageContext.request.contextPath}/xxx/noticeView.jsp?FileType=.doc&EditType=1&Template='+template+'¬iceOrgName='+encodeURIComponent(noticeOrgName)+'¬iceOrgNo='+noticeOrgNo+'¬iceContent='+encodeURIComponent(noticeContent)+'¬iceDate='+noticeDate;window.open(url, '_blank');}}
到另外 的一個頁面也需要進行替換那些斷行符號換行回來
var noticeContent='<%=noticeContent %>'.replace(/\<br>/g,"\r");
提交轉碼參考下面
js對文字進行編碼涉及3個函數:escape,encodeURI,encodeURIComponent,相應3個解碼函數:unescape,decodeURI,decodeURIComponent
1、 傳遞參數時需要使用encodeURIComponent,這樣組合的url才不會被#等特殊字元截斷。
例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');</script>
2、 進行url跳轉時可以整體使用encodeURI
例如:Location.href="/encodeURI"("http://cang.baidu.com/do/s?word=百度&ct=21");
3、 js使用資料時可以使用escape
例如:搜藏中history紀錄。
4、 escape對0-255以外的unicode值進行編碼時輸出%u****格式,其它情況下escape,encodeURI,encodeURIComponent編碼結果相同。
最多使用的應為encodeURIComponent,它是將中文、韓文等特殊字元轉換成utf-8格式的url編碼,所以如果給後台傳遞參數需要使用encodeURIComponent時需要後台解碼對utf-8支援(form中的編碼方式和當前頁面編碼方式相同)
escape不編碼字元有69個:*,+,-,.,/,@,_,0-9,a-z,A-Z
encodeURI不編碼字元有82個:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURIComponent不編碼字元有71個:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
根據說明 我需要的是encodeURIComponent函數