jQuizMe 2.1 Release Page


Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

This holiday break gave me the time that I need to push out a next release of jQuizMe.
Check it out at Google Code, jQuizMe.

Please leave your feedback in the comment box.

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Javascript Tip: Convert string to unicode values


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

The following script will convert a string to the unicode values using javascript.
toUnicode is a extension to String.charCodeAt. The benefit of this function is that it returns the whole string a html unicode format.

Code

1
2
3
4
5
6
7
8
String.prototype.toUnicode = function () {
    var uni = [],
        i = this.length;
    while (i--) {
        uni[i] = this.charCodeAt(i);
    }
    return "&#" + uni.join(';&#') + ";";
};

Here’s another method that I thought of.
This one is using a regexp instead of a while loop and an array to form the unicode text.

1
2
3
4
5
String.prototype.toUnicode = function () {
    return this.replace(/./g, function (char) {
        return "&#" + String.charCodeAt(char) + ";";
    });
};

Usage:
Call toUnicode() on any string object.

1
obj.toUnicode();

Example:

1
<li/>”.toUnicode() //outputs: &#60;&#108;&#105;&#47;&#62;

However, if possible use document.createTextNode to insert text instead of converting it to unicode. Document.createTextNode is a native DOM method meaning it’s a lot faster, and easier to understand .
More infor here: document.createTextNode

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube