JavaScript: Image onerror Fallback

Published on

Without a JavaScript function <img src=”image.png” onError=”this.onerror=null;this.src=’/images/noimage.gif’;” /> or With a JavaScript function function imgError(image) { image.onerror = “”; image.src = “/images/noimage.gif”; return true; } <img src=”image.png” onerror=”imgError(this);”/> For a compatibility lists of browsers that support the error facility: http://www.quirksmode.org/dom/events/error.html

JavaScript: stripTags

Published on

/* Strip html tags. reference: http://davidwalsh.name/essential-javascript-functions */ function stripTags(htmlstr) { var div = document.createElement(‘div’); div.innerHTML = htmlstr; return div.textContent; }

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: 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