Category Archives: Javascript Snippets

Color validation

function validateColor(color) { var el, result;   el = document.createElement("div"); el.style.backgroundColor = "white";   document.body.appendChild(el);   el.style.backgroundColor = color; result = el.style.backgroundColor; el.parentNode.removeChild(el);   return color === "white" || result !== "white"; }   validateColor(’white’);

Formula for calculating size of the scrollbar

The formula is very simple, and returns almost identic results (tested on OSX). size = (element_height / element_scroll_height) * element_height and similar for horizontal one: size = (element_width / element_scroll_width) * element_width In JS: var size = (el.offsetHeight / el.scrollHeight) * el.offsetHeight; In JS + JQ: var size = ($el.height() / el.scrollHeight) * $el.height();