Skip to content

Instantly share code, notes, and snippets.

View andreasvirkus's full-sized avatar
made you look

andreas andreasvirkus

made you look
View GitHub Profile
function rand(min, max, integer) {
var r = Math.random() * (max - min) + min;
return integer ? r|0 : r;
// Usage
console.log(rand(2,5)); // float random between 2 and 5 inclusive
console.log(rand(1,100,true)); // integer random between 1 and 100
function stripTags(htmlstr) {
var div = document.createElement('div');
div.innerHTML = htmlstr;
return div.textContent;
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
return result;
convert -strip -quality 80% original.jpg step1.jpg
jpegtran -optimize -progressive -copy none -outfile output.jpg step1.jpg
// Meant to find out if a DOM element is scrolled into view
// aka if it is visible on the screen.
function appear(elem) {
var $elem = $(elem);
var $window = $(window);
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
// add this as the only script before your closing body tag
// and let it load your external scripts once your DOM has finished loading.
function addJSOnload() {
var element = document.createElement("script");
element.src = "defer.js"; // replace your script with "defer.js"
if (window.addEventListener)
window.addEventListener("load", addJSOnload, false);
else if (window.attachEvent)
/* A simple Bootstrap clearfix mod */
/* Tablet */
@media (min-width:767px){
/* Column clear fix */
//For the times that I need to know how far an element is from the top of the DOM,
//not the top of it’s parent, I use this helper function.
//Loops through all parent nodes of an element to get it's distance from the top of the document
function getDistanceFromTop(element) {
var yPos = 0;
while(element) {
yPos += (element.offsetTop);
element = element.offsetParent;
# Will print the 3rd line of all script files in the given folder (except 'myscripts')
for file in ~/scripts/*[^~"myscripts"]
tail -n+3 $file | head -n1