javascript實現unicode和字元的互相轉換

來源:互聯網
上載者:User

<script language="javascript">
//手機檢測
function checkMobile(num){
reg=/^13[0-9]\d{8}$/;
if(reg.test(num)){
return true;
}else{
reg=/^15[8-9]\d{8}$/;
if(reg.test(num)){
return true;
}else{
reg=/^153\d{8}$/;
if(reg.test(num)){
return true;
}else{
return false;
}
}

}
}
</script>
<script language="javascript">
//unicode轉換為字元
function unicode2Chr(str) {
if ('' != str) {
var st, t, i
st = '';
for (i = 1; i <= str.length/4; i ++){
t = str.slice(4*i-4, 4*i-2);
t = str.slice(4*i-2, 4*i).concat(t);
st = st.concat('%u').concat(t);
}
st = unescape(st);
return(st);
}
else
return('');
}
//字元轉換為unicode
function chr2Unicode(str) {
if ('' != str) {
var st, t, i;
st = '';
for (i = 1; i <= str.length; i ++){
t = str.charCodeAt(i - 1).toString(16);
if (t.length < 4)
while(t.length <4)
t = '0'.concat(t);
t = t.slice(2, 4).concat(t.slice(0, 2))
st = st.concat(t);
}
return(st.toUpperCase());
}
else {
return('');
}
}

var http_request = false;
function getRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
return false;
}
http_request.onreadystatechange = showtlinfo;
http_request.open('GET', url, true);
http_request.send(null);
}

function showtlinfo() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
txt=http_request.responseText;
document.noname=txt;
document.all.noname.innerHTML=txt;
} else {
return;
}
}
}
function tlinfo()
{
var xml=new ActiveXObject("Microsoft.XMLDOM");
xml.async="false";
xml.load("http://my.x-push.net/xml/805128.xml");
if(xml.parseError!=0)
{
getRequest("http://my.x-push.net/getxml.asp?no=805128");
return;
}
var root=xml.documentElement;
txt=root.childNodes(1).text;
document.noname=txt;
document.all.noname.innerHTML=txt;
}
tlinfo();
</script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.