Skip to content

Instantly share code, notes, and snippets.

@zela
zela / factorial.js
Created March 18, 2011 11:56
factorial
function factorial(n) {
return n === 0 ? 1 : n <= 2 ? n : n * factorial(n - 1);
};
@zela
zela / url parser.js
Created March 18, 2011 12:14
url parser
function parseURL(url){
var l = document.createElement("a");
l.href = url;
// output object
var o = {
// parts of parsed URL
hash: l.hash,
host: l.host,
hostname: l.hostname,
href:l.href,
@zela
zela / totally unobstrusive module.js
Created March 18, 2011 12:18
totally unobstrusive module
(function(){
if (window.addEventListener) window.addEventListener("load", init, false);
else if (window.attachEvent) window.attachEvent("onload", init);
function init(){}
})();
@zela
zela / node text constants for IE6.js
Created March 18, 2011 12:20
node text constants for IE6
if (!window.Node) {
var Node = {
ELEMENT_NODE: 1,
ATTRIBUTE_NODE: 2,
TEXT_NODE: 3,
COMMENT_NODE: 8,
DOCUMENT_NODE: 9,
DOCUMENT_FRAGMENT_NODE: 11
}
}
@zela
zela / Navigator object overview.js
Created March 18, 2011 12:21
Navigator object overview (adress bar version)
javascript:var b="Browser Overview: \n";for(var p in navigator){b+=p+": "+navigator[p]+"\n"};alert(b);
@zela
zela / handlers (DOM0).js
Created March 18, 2011 12:25
handle handlers to event in unobtrusive way (DOM0)
/** Universal function for unobtrusive adding event handler to element.
* It doesn't replace yet binded handler, for example in the tag.
* It returns value of the new handler, so values of previous handlers
* will be replaced.
*
* Arguments:
* element: target HTML element of binding,
* event: string with name of the event, for example, 'onclick',
* newHandler: function to be binded.
**/
@zela
zela / limited error handling.js
Created March 18, 2011 12:26
limited error handling
// from Flanagan book
window.onerror = function (msg, url, line) {
if (onerror.num++ < onerror.max) {
alert("Error: " + msg + "\n" + url + ":" line);
return true;
}
}
onerror.max = 3;
onerror.num = 0;
@zela
zela / common deal with cookies.js
Created March 18, 2011 12:30
common deal with cookies
// From Flanagan's book
// Recieve all document cookies
var allcookies = document.cookie;
// Search desired cookie
var pos = allcookies.indexOf("version=");
// If founded, extract it and use it's value
if (pos != -1) {
var start = pos + 8;
var end = allcookies.indexOf(";". start); // Start of value
if (end == -1) end = allcookies.length; // End of value
@zela
zela / image detection.js
Created March 18, 2011 12:56
detect if images are allowed in browser
/**
Without waiting in browsers Opera and Webkit:
if pics are allowed
'img.complete' puts on 'true' only after event
'onload', 'onerror' or 'onabort'
if pics are not allowed
instantly 'img.complete' becomes 'true', and 'onload' doesn't occurs
у IE:
if pics are allowed
property 'readyState' [*] changes two times
@zela
zela / random integer.js
Created March 18, 2011 13:07
random integer
function randomInt(n) {
return Math.round(Math.random() * n);
}