There are many methods, for example:
First: (use the charcodeat method of the string object)
String. prototype. getbyteslength = function () {<br/> var length = 0; <br/> for (I = 0; I <this. length; I ++) {<br/> icode = This. charcodeat (I); <br/> If (icode> = 0 & icode <= 255) | (icode> = 0xff61 & icode <= 0xff9f )) {<br/> Length + = 1; <br/>} else {<br/> Length + = 2; <br/>}< br/> return length; <br/>}
Second: (use the escape () method to convert the code and then determine)
String. prototype. getbyteslength = function () {<br/> var STR = escape (this); <br/> for (VAR I = 0, length = 0; I <Str. length; I ++, length ++) {<br/> If (Str. charat (I) = "%") {<br/> If (Str. charat (++ I) = "U") {<br/> I + = 3; <br/> length ++; <br/>}< br/> I ++; <br/>}< br/> return length; <br/>}
The third method is completely speechless!
String. prototype. getbyteslength = function () {<br/> return this. replace (/[^/x00-/xFF]/GI ,"--"). length; <br/>}
I like the third method. All the above Code has passed the test.
Simple code with no test results