首頁 >
文章 > web前端 > javascript实现unicode和字符的互相转换_javascript技巧
javascript实现unicode和字符的互相转换_javascript技巧
- WBOY原創
- 2016-05-16 19:11:20898瀏覽
<script> <BR>//unicode转换为字符 <BR>function unicode2Chr(str) { <BR> if ('' != str) { <BR> var st, t, i <BR> st = ''; <BR> for (i = 1; i <= str.length/4; i ++){ <BR> t = str.slice(4*i-4, 4*i-2); <BR> t = str.slice(4*i-2, 4*i).concat(t); <BR> st = st.concat('%u').concat(t); <BR> } <BR> st = unescape(st); <BR> return(st); <BR> } <BR> else <BR> return(''); <BR>} <BR>//字符转换为unicode <BR>function chr2Unicode(str) { <BR> if ('' != str) { <BR> var st, t, i; <BR> st = ''; <BR> for (i = 1; i <= str.length; i ++){ <BR> t = str.charCodeAt(i - 1).toString(16); <BR> if (t.length < 4) <BR> while(t.length <4) <BR> t = '0'.concat(t); <BR> t = t.slice(2, 4).concat(t.slice(0, 2)) <BR> st = st.concat(t); <BR> } <BR> return(st.toUpperCase()); <BR> } <BR> else { <BR> return(''); <BR> } <BR>} <br><br>var http_request = false; <BR>function getRequest(url) { <BR> http_request = false; <BR> if (window.XMLHttpRequest) { // Mozilla, Safari,... <BR> http_request = new XMLHttpRequest(); <BR> if (http_request.overrideMimeType) { <BR> http_request.overrideMimeType('text/xml'); <BR> } <BR> } else if (window.ActiveXObject) { // IE <BR> try { <BR> http_request = new ActiveXObject("Msxml2.XMLHTTP"); <BR> } catch (e) { <BR> try { <BR> http_request = new ActiveXObject("Microsoft.XMLHTTP"); <BR> } catch (e) {} <BR> } <BR> } <BR> if (!http_request) { <BR> return false; <BR> } <BR> http_request.onreadystatechange = showtlinfo; <BR> http_request.open('GET', url, true); <BR> http_request.send(null); <BR>} <br><br>function showtlinfo() { <BR> if (http_request.readyState == 4) { <BR> if (http_request.status == 200) { <BR> txt=http_request.responseText; <BR> document.noname=txt; <BR> document.all.noname.innerHTML=txt; <BR> } else { <BR> return; <BR> } <BR> } <BR>} <BR>function tlinfo() <BR>{ <BR> var xml=new ActiveXObject("Microsoft.XMLDOM"); <BR> xml.async="false"; <BR> xml.load("http://my.x-push.net/xml/805128.xml"); <BR> if(xml.parseError!=0) <BR> { <BR> getRequest("http://my.x-push.net/getxml.asp?no=805128"); <BR> return; <BR> } <BR> var root=xml.documentElement; <BR> txt=root.childNodes(1).text; <BR> document.noname=txt; <BR> document.all.noname.innerHTML=txt; <BR>} <BR>tlinfo(); <BR></script>
陳述:本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn