提交鏈接時候中文轉碼以及回車替換

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函數



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章