JavaScript: poll

Published on

/* Usage: ensure element is visible poll( function() { return document.getElementById(‘lightbox’).offsetWidth > 0; }, function() { // Done, success callback }, function() { // Error, failure callback } ); reference: http://davidwalsh.name/essential-javascript-functions */ function poll(fn, callback, errback, timeout, interval) { var endTime = Number(new Date()) + (timeout || 2000); interval = interval || 100; (function p() … Continue reading JavaScript: poll

JavaScript: once

Published on

/* Execute a function only once. reference: http://davidwalsh.name/essential-javascript-functions */ function once(fn, context) { var result; return function() { if(fn) { result = fn.apply(context || this, arguments); fn = null; } return result; }; }

JavaScript: matchesSelector

Published on

/* Returns true if the specified selector matches. Usage: matchesSelector(document.getElementById(‘myDiv’), ‘div.someSelector[some-attribute=true]’) reference: http://davidwalsh.name/essential-javascript-functions */ function matchesSelector(el, selector) { var p = Element.prototype; var f = p.matches || p.webkitMatchesSelector || p.mozMatchesSelector || p.msMatchesSelector || function(s) { return [].indexOf.call(document.querySelectorAll(s), this) !== -1; }; return f.call(el, selector); }

JavaScript: isNative

Published on

/* Determines if a JavaScript function is native to a browser. Usage: isNative(alert); // true isNative(myCustomFunction); // false reference: http://davidwalsh.name/essential-javascript-functions */ (function() { // Used to resolve the internal `[[Class]]` of values var toString = Object.prototype.toString; // Used to resolve the decompiled source of functions var fnToString = Function.prototype.toString; // Used to detect host constructors … Continue reading JavaScript: isNative

JavaScript: insertRule

Published on

/* Inserts a CSS style rule. Usage: sheet.insertRule(“header { float: left; opacity: 0.8; }”, 1);false reference: http://davidwalsh.name/essential-javascript-functions */ var sheet = (function() { // Create the <style> tag var style = document.createElement(‘style’); // Add a media (and/or media query) here if you’d like! // style.setAttribute(‘media’, ‘screen’) // style.setAttribute(‘media’, ‘only screen and (max-width : 1024px)’) // … Continue reading JavaScript: insertRule

JavaScript: htmlEntities

Published on

/* Replaces html entities. */ function htmlEntities(str) { return String(str).replace(/&/g, ‘&amp;’).replace(/</g, ‘&lt;’).replace(/>/g, ‘&gt;’).replace(/”/g, ‘&quot;’); }

JavaScript: hideaddressbar

Published on

/* * Normalized hide address bar for iOS & Android * (c) Scott Jehl, scottjehl.com * MIT License */ (function( win ){ var doc = win.document; // If there’s a hash, or addEventListener is undefined, stop here if( !location.hash && win.addEventListener ){ //scroll to 1 window.scrollTo( 0, 1 ); var scrollTop = 1, getScrollTop = … Continue reading JavaScript: hideaddressbar

JavaScript: getUrlParameter

Published on

/* Returns the value of a url parameter. reference: http://davidwalsh.name/essential-javascript-functions */ function getUrlParameter(sParam) { var sPageUrl = window.location.search.substring(1); var sUrlVariables = sPageUrl.split(‘&’); for (var i = 0; i < sUrlVariables.length; i++) { var sParameterName = sUrlVariables[i].split('='); if (sParameterName[0] == sParam) { return sParameterName[1]; } } return null; }

JavaScript: getAbsoluteUrl

Published on

/* Returns the absolute url. Usage: getAbsoluteUrl(‘/something’); http://davidwalsh.name/something reference: http://davidwalsh.name/essential-javascript-functions */ var getAbsoluteUrl = (function() { var a; return function(url) { if(!a) a = document.createElement(‘a’); a.href = url; return a.href; }; })();