//獲取字符串的長度 String.prototype.getByteLength = function() { var bytes=0,i=0; for (; i<this.length; ++i,++bytes) { if ( this.charCodeAt(i) > 255 ) { ++bytes; } } return bytes; }; /*函數調用方法*/ var a ="aaa ^&*"; var length = a.getByteLength(); alert(length);