Skip to content

Instantly share code, notes, and snippets.

Avatar

Dustin Poissant dustinpoissant

  • Burlington, VT
View GitHub Profile
View strinToDom.func.js
const stringToDOM = str => new DOMParser().parseFromString(str, 'text/html').body.childNodes[0];
@dustinpoissant
dustinpoissant / cookies.js
Created Nov 27, 2019
Get, Set and Delete Cookies
View cookies.js
function getCookie(name) {
var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return v ? v[2] : null;
}
function setCookie(name, value, days) {
if(!days) var days = 365;
var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days);
document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
@dustinpoissant
dustinpoissant / String.extract.func.js
Created Dec 1, 2017
Extract a substring from a string between two substrings.
View String.extract.func.js
String.prototype.extract = function(start, finish){
var s = this.indexOf(start);
if(s == -1) return "";
var e = this.indexOf(finish, s + start.length);
if(e == -1) return "";
return this.substring(s + start.length, e);
}
@dustinpoissant
dustinpoissant / animateTransform.jquery.js
Last active Jul 2, 2018
jQuery Plugin to Animate Transforms
View animateTransform.jquery.js
$.fn.animateTransform = function(/* [start,] end [, duration] [, callback] */){
var start = null, end = null, duration = 400, callback = function(){};
for(var i=0; i<arguments.length; i++){
if(typeof(arguments[i]) == "string"){
if(!start) start = arguments[i];
else end = arguments[i];
} else if(typeof(arguments[i]) == "number"){
duration = arguments[i];
} else if(typeof(arguments[i]) == "function"){
callback = arguments[i];
@dustinpoissant
dustinpoissant / selectText.jquery.js
Created Feb 7, 2017
Selects the text within an element.
View selectText.jquery.js
jQuery.fn.selectText = function(){
var doc = document;
var element = this[0];
if (doc.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
@dustinpoissant
dustinpoissant / attrs.jquery.js
Created Jan 31, 2017
Get all attributes of an element.
View attrs.jquery.js
$.fn.attrs = function(){
var attributes = {};
if(this.length){
$.each( this[0].attributes, function( index, attr ) {
attributes[ attr.name ] = attr.value;
});
}
return attributes;
};
@dustinpoissant
dustinpoissant / roundTo.func.js
Created Jan 24, 2017
Round a number to the nearest "X"
View roundTo.func.js
function roundTo(number, to){
return (Math.round(number * (1/to)) / (1/to));
};
@dustinpoissant
dustinpoissant / containsObject.func.min.js
Created Nov 22, 2016
A quick and dirty check to see of one object contains another.
View containsObject.func.min.js
function containsObject(a,b){for(var c in a)if(!b[c]||a[c]!=b[c])return!1;return!0}
@dustinpoissant
dustinpoissant / validCSS.func.js
Last active Nov 28, 2016
Check if a CSS property/value is valid.
View validCSS.func.js
function validCSS(property, value){
var $el = $("<div></div>");
$el.css(property, value);
return ($el.css(property) == value);
}
@dustinpoissant
dustinpoissant / betterParseFloat.func.js
Last active Dec 1, 2016
Pasrses a number better than the orginal parseFloat function
View betterParseFloat.func.js
function betterParseFloat(num){
num = (num+"").replace(new RegExp(",", 'g'), ""); // Remove commas
return (num.length==0)?0:(isNaN(parseFloat(num)))?betterParseFloat(num.substr(1)):parseFloat(num);
};
You can’t perform that action at this time.