Forum

Heyxu Forum

Forum Dictionary / Programming / 網頁/網站程式 / JavaScript /

Javascript 中有沒有 PHP 的 urlencode 函式

New Subject
Random
Previous
Next
|
Javascript 中有沒有 PHP 的 urlencode 函式

我想在傳送的單位資料裡加入類似 PHP urlencode 的函式,先在 Javascript 再送到後端去,有大大知道 Javascript 有沒有相同功能的函式?
  • Keywords : 函式, Javascript, 相同功能, 大大, 傳送, urlencode
0 0
2008-11-26T18:57:15+0000

黑目狀態 :

之前找到的用Javascript 寫的替代函式:

<script language="javascript">
function UrlEncode(str){
  var ret="";
  var strSpecial="!"#$%&'()* ,/:;<=>?[]^`{|}~%";
  for(var i=0;i<str.length;i ){
   var chr = str.charAt(i);
    var c=str2asc(chr);
    tt = chr ":" c "n";
    if(parseInt("0x" c) > 0x7f){
      ret ="%" c.slice(0,2) "%" c.slice(-2);
    }else{
      if(chr==" ")
        ret =" ";
      else if(strSpecial.indexOf(chr)!=-1)
        ret ="%" c.toString(16);
      else
        ret =chr;
    }
  }
  return ret;
}
function UrlDecode(str){
  var ret="";
  for(var i=0;i<str.length;i ){
   var chr = str.charAt(i);
    if(chr == " "){
      ret =" ";
    }else if(chr=="%"){
     var asc = str.substring(i 1,i 3);
     if(parseInt("0x" asc)>0x7f){
      ret =asc2str(parseInt("0x" asc str.substring(i 4,i 6)));
      i =5;
     }else{
      ret =asc2str(parseInt("0x" asc));
      i =2;
     }
    }else{
      ret = chr;
    }
  }
  return ret;
}
</script>

0 0
2008-11-28T19:31:32+0000

魚戀秋波 Neil Y.K. :

其實可以用Javascript 提供的 escape() 這個函式就可以囉!
其他相關的函式還有 encodeURI 跟 decodeURI.
不同自己寫額外的函式唷!
0 0
2008-11-29T18:12:10+0000


  • Now, you can post comments by Facebook Account when your Heyxu account was logout or unvariable. whatever, we suggest to post comment by Heyxu Account to get more bounds to join new events of Heyxu
Comment
Verify
Comment
  • Auto Platform
    For better browsing experience, it will auto detects your device to display webpage.
  • Bug Report
    Tell us about the idea, problem and errors you found.
  • Copyright
    © 2009 Heyxu Multimedia, Inc - All rights reserved.
  • Revised Version
    V2.1.356
    15 Jul 2019 (GMT+8)