Skip to content

Instantly share code, notes, and snippets.

@davidbarredo
davidbarredo / svgSupport.js
Created Dec 19, 2016
One line to detect SVG support (usually for IE8)
View svgSupport.js
var svgSupport = !!(document.createElementNS && document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGRect);
@davidbarredo
davidbarredo / app.js
Created Nov 21, 2016 — forked from Turbo87/app.js
webpack + font-awesome test
View app.js
require('font-awesome/css/font-awesome.css');
document.body.innerHTML = '<i class="fa fa-fw fa-question"></i>';
@davidbarredo
davidbarredo / extendBackbone.js
Created Oct 8, 2015
How to extend properly Backbone views, models and collections
View extendBackbone.js
var obj = {
foo: function() {
// do awesome things
},
bar: function() {
// do magic stuff
}
};
var collection = Backbone.Collection.extend({});
@davidbarredo
davidbarredo / copyWithElementInvisible.js
Created Oct 7, 2015
Copy info to clipboard with javascript
View copyWithElementInvisible.js
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
//
// *** This styling is an extra step which is likely not required. ***
//
// Why is it here? To ensure:
// 1. the element is able to have focus and selection.
// 2. if element was to flash render it has minimal visual impact.
// 3. less flakyness with selection and copying which **might** occur if
@davidbarredo
davidbarredo / utilRegex.js
Last active Oct 7, 2015
Util regular expressions
View utilRegex.js
//for positive integers
/^\+?[1-9]\d*$/.test();
//for positive integers and zero
/^\+?(0|[1-9]\d*)$/.test();
//for positive/negative integers and zero
/^\-?(0|[1-9]\d*)$/.test();
//to match specific chars
@davidbarredo
davidbarredo / emptyArray.js
Last active Sep 15, 2015
Empty an array methods
View emptyArray.js
A = [];
// This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array.
A.length = 0;
// This will clear the existing array by setting its length to 0
A.splice(0,A.length);
// Using .splice() will work perfectly, but since .splice() function will return an array with all the removed items, it will actually return a copy of the original array. Benchmarks suggests that this has no effect on performance whatsoever.
while(A.length > 0) {
@davidbarredo
davidbarredo / IE8_and_below_patch.js
Last active Aug 29, 2015
Determine whether an array contains a value
View IE8_and_below_patch.js
var indexOf = function(needle) {
if(typeof Array.prototype.indexOf === 'function') {
indexOf = Array.prototype.indexOf;
} else {
indexOf = function(needle) {
var i = -1, index = -1;
for(i = 0; i < this.length; i++) {
if(this[i] === needle) {
index = i;
@davidbarredo
davidbarredo / endResize.js
Created Nov 19, 2013
Detect end of window resize (with timer help)
View endResize.js
window.onresize = function() {
if ( this.resizeTimer ) clearTimeout( this.resizeTimer );
this.resizeTimer = setTimeout ( function() {
/* Your code here */
}, 500 );
}
@davidbarredo
davidbarredo / insertGist.js
Created Nov 19, 2013
Dynamically add a script of a github gist
View insertGist.js
/* Create script element */
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://gist.github.com/7544705.js';
/* Backup document.write function */
if(!document._write) document._write = document.write;
/* Override document.write function */
document.write = function (str) {
document.getElementById('scriptCont').innerHTML += str;
};
@davidbarredo
davidbarredo / dateExtensions.js
Last active Dec 28, 2015
Util date extensions
View dateExtensions.js
// Remember to prototype your own custom Date
var customDate = Date
// Date extension to to make monday day 0 (week start on monday)
customDate.prototype.getDayL = function () {
if ( this.getDay() == 0 ) {
return 6;
}else{
return this.getDay() - 1;
}