Category Archives: micro tutorial

JavaScript encode cyrillic symbols with encodeURIComponent

JavaScript and Cyrillic

Let’s assume we’ve a JavaScript file which rewrites the location.href, resulting in changes in the URI of the browser. Everything is OK till you work with Latin alphabet. The problem arises when you decide to put some Cyrillic symbols into the string.

The encode() function

With that function you can encode the URL if you have some entity symbols. It is not like but it should be something like encodeURI in PHP. The problem is that this function does not work correctly with Cyrillic symbols.

Use encodeURIComponent

There is another usefull function which does this. It’s called encodeURIComponent, and I strongly recomment its use.

JavaScript random numbers

The task is …

to generate a random number between any two interger unsigned numbers. Let’s assume we’ve two numbers ‘a’ and ‘b’, where b > a. (I don’t count the case with b==a, cause it seems to be pretty strange!). The formula should be like that:

Math.random() * (b-a+1) + a

Looks very nice, but why is that?

because simply by using Math.random() * a gives a random between 0 and a-1. With the expresion (b-a+1) gives a random number between 0 and (b-a+1) and than by adding the value of a we’ve the number between a and b.

Remove DOM Element with JQuery

JQuery and DOM

JQuery is one of the most famous JavaScript library online. There are so much articles in the web about it, that I feel useless to describe it again. The good parts of that library is the amazing way it handles the DOM. It is so good at that that I usually use it with other JavaScript libraries.

Now I’m working on a task and one of the main target of the completed task was the RAM usage of the code. It just has about 100 dynamic DOM elements which on every event should be removed form the tree and the RAM should be freed. Continue reading Remove DOM Element with JQuery →