RSS
 

Archive for the ‘Web Development’ Category

Detect if Internet Explorer and Its Version Number Easily with Pure JavaScript

25 Feb

I decided to share a useful one-row snippet for web developers, which can save time and complexity using JavaScript that checks whether the browser the code runs on is MSIE (Internet Explorer) and what version is it.

1
var MSIE = parseInt(window.navigator.userAgent.split("MSIE ")[1]) || false;

Now the MSIE variable holds the version of the IE browser, or false in case of other browser.

1
2
3
4
// i.e. atob() function supported on all browsers except IE9 and lower.
if ( !MSIE || MSIE > 9 ) {
    alert(atob("WW91J3JlIEF3ZXNvbWUh"));
}

Enjoy!

 

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 ([email protected]).
 * @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 »