• 未分類
  • 2

FLASH 8 使用 URL encode

在網頁傳值的時候常常會用到 URL encode

但FLASH 8本身 沒有提供URLencode的方法(不過CS3有)

decodeURI()
decodeURIComponent()
encodeURIComponent()

這幾種方式可以用,不過只有CS3 才有

所以弄了一個擴充 String 的方法


String.prototype.encodeURIComponent = function() {
var Result = [];
var len = this.length;
for (var i = 0; i Result.push(this.charAt(i).toUtf8());
}
return Result.join("");
};
String.prototype.toUtf8 = function() {
var code = this.charCodeAt(0);
if (code return "%0" + code.toString(16);
}
var iByte = 0;
var i = 0;
var Result = "";
while (code > 0x7f) {
iByte = code % 0x40;
code = (code - iByte) / 0x40;
Result = "%" + (iByte | 0x80).toString(16).toUpperCase() + Result;
i++;
}
prefix = [0x0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc];
if (i > prefix.length) {
i = 5;
}
Result = "%" + (code | prefix[i]).toString(16).toUpperCase() + Result;
return Result;
};
String.prototype.encodeURI = function() {
var result = [];
var len = this.length;
for (var i = 0; i var code = this.charCodeAt(i);
if (code //if (code == 61 || code == 38) {
result.push(this.charAt(i));
} else {
result.push(this.charAt(i).toUtf8());
}
}
return result.join("");
};
var st = "http://220.133.52.91/羊小咩?a=10&b=lamb-mei";
trace(st.encodeURI());
trace(st.encodeURIComponent());
String.prototype.encodeURIComponent = function() {
var Result = [];
var len = this.length;
for (var i = 0; i
Result.push(this.charAt(i).toUtf8());

}
return Result.join("");
};
String.prototype.toUtf8 = function() {
var code = this.charCodeAt(0);
if (code return "%0" + code.toString(16);
}
var iByte = 0;
var i = 0;
var Result = "";
while (code > 0x7f) {
iByte = code % 0x40;
code = (code - iByte) / 0x40;
Result = "%" + (iByte | 0x80).toString(16).toUpperCase() + Result;
i++;
}
prefix = [0x0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc];
if (i > prefix.length) {
i = 5;
}
Result = "%" + (code | prefix[i]).toString(16).toUpperCase() + Result;
return Result;
};
String.prototype.encodeURI = function() {
var result = [];
var len = this.length;
for (var i = 0; i var code = this.charCodeAt(i);
if (code //if (code == 61 || code == 38) {
result.push(this.charAt(i));
} else {
result.push(this.charAt(i).toUtf8());
}
}
return result.join("");
};
var st = "http://220.133.52.91/羊小咩?a=10&b=lamb-mei";
trace(st.encodeURI());
trace(st.encodeURIComponent());

output


http://220.133.52.91/%E7%BE%8A%E5%B0%8F%E5%92%A9?a=10&b=lamb-mei
%68%74%74%70%3A%2F%2F%32%32%30%2E%31%33%33..(太長了)

有兩種方式

String.encodeURI ()

String..encodeURIComponent();
一種只會將特殊字.. Unicode 轉換成UTF-8

另外一種是全部都轉換…

P.S 新的值都是直接retrun 所以用的時候要

var str = “http://220.133.52.91/羊小咩?a=10&b=lamb-mei”;
var newstr = str.encodeURI();

Comments

comments

您可能也會喜歡…

2 個回應

  1. Anonymous表示:

    as2 用escape(“羊小咩’s 咩仔部落格”),就可以了……

  2. 羊小咩表示:

    用escape是OK的
    不過用escape對需要用兩個bit的字元解釋會有問題,舉如說後端接收解析會變成亂碼,所以那時後才用這種方式去讓他可以正常使用

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料