RSS
 

Convert Base 10 to Base 64 and vise versa using JavaScript

13 Jan

This is the way to convert base 10 (a decimal number) into a base 64 string and base 64 into a base 10, while the order of that base is as following:

0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-

Base 10 to Base 64

1
2
3
4
5
6
7
8
9
10
11
12
function base10_to_base64(num) {
    var order = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
    var base = order.length;
    var str = "", r;
    while (num) {
        r = num % base
        num -= r;
        num /= base;
        str = order.charAt(r) + str;
    }
    return str;
}

Read the rest of this entry »

 

Test if the given value is a decimal number or not in JavaScript using Regular Expression

26 May

To identify in JavaScript whether the number or a string is in a decimal format or not, is actually pretty easy and short.
It looks obvious, but actually it doesn’t due to several different combinations of decimal number representation in JavaScript.

I’m using Regular Expression to test it:

1
2
3
function isDecimal(number) {
    return /^\d+(\.\d+|e)(e?-\d+)?$/.test(number);
}

Read the rest of this entry »

 

The efficient way to create GUID/UUID in JavaScript (with explanation)

22 May

I want to share with you the fastest GUID/UUID generator method (as far as I know).

Million executions of this implementation takes just 32.5 seconds, which is the fastest i’ve ever seen in a browser (the only solution without loops/iterations).

The function is as simple as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * Generates a GUID string.
 * @returns {String} The generated GUID.
 * @example af8a8416-6e18-a307-bd9c-f2c947bbb3aa
 * @author Slavik Meltser (slavik@meltser.info).
 * @link http://slavik.meltser.info/?p=142
 */

function guid() {
    function _p8(s) {
        var p = (Math.random().toString(16)+"000000000").substr(2,8);
        return s ? "-" + p.substr(0,4) + "-" + p.substr(4,4) : p ;
    }
    return _p8() + _p8(true) + _p8(true) + _p8();
}

Read the rest of this entry »

 

Make the radio buttons behave like single-choice checkboxes

27 Nov

If you a web developer, maybe you encountered this annoying thing.

Let’s say you need to write a list of choices, i.e. questions – answers. Let’s define that all the questions are optional and you can select just one answer per question. The way you should implement it, is using inputs with radio type, that’s fine and this how it should be.
Example code is:

1
2
3
4
5
6
7
8
9
10
11
<form method="post">
    <h4>What is favorite time?</h4>
    <label><input type="radio" name="question1" value="morning" /> Morning</label>
    <label><input type="radio" name="question1" value="noon" /> Noon</label>
    <label><input type="radio" name="question1" value="evening" /> Evening</label>
    <h4>What is your preferable paying method?</h4>
    <label><input type="radio" name="question2" value="cc" /> Credit Card</label>
    <label><input type="radio" name="question2" value="check" /> Check</label>
    <label><input type="radio" name="question2" value="paypal" /> Paypal</label>
    <label><input type="radio" name="question2" value="cash" /> Cash</label>
</form>

Read the rest of this entry »

 

PhoneGap – Detect if the application runs on mobile or browser using JavaScript before the “deviceready” and document “ready” events are triggered

05 Nov

I spent hours to find out how to do it. And believe me or not, I found nothing on the whole internet about how to solve it.

Actually there is a simple way to do that after the event “deviceready” has been triggered (as described on the PhoneGap’s API Docs), it is simple as:

1
2
3
4
5
6
7
document.addEventListener("deviceready", function(e){
if ( device ) {
alert("Running on PhoneGap!");
} else {
alert("Running NOT on PhoneGap!");
}
}, false);

Read the rest of this entry »

 

Synergy – Using same keyboard and mouse on 2 different PC/OS

21 Nov

As you know or don’t know, all my work (especially programming) is done from a laptop running Linux OS, or to be more specific, Fedora.
Except from programming, I’m also using Adobe products like Photoshop, Fireworks and more to design and style photos, picture and images.
As I know for now, there is no standard way to run Adobe products from Linux but only from Windows and Mac OS X.
Read the rest of this entry »

 
37 Comments

Posted in Software