Skip to content

Instantly share code, notes, and snippets.

View dustinpoissant's full-sized avatar

Dustin Poissant dustinpoissant

View GitHub Profile
@dustinpoissant
dustinpoissant / hasAttr.min.js
Created November 19, 2016 22:05
jQuery hasAttr
$.fn.hasAttr=function(a){var b=$(this).attr(a);return void 0!==typeof b&&b!==!1};
@dustinpoissant
dustinpoissant / Date.valid.js
Last active December 23, 2020 11:44
Determine if a string is a validate Date
Date.valid = function(str){
var d = new Date(str);
return (Object.prototype.toString.call(d) === "[object Date]" && !isNaN(d.getTime()));
}
@dustinpoissant
dustinpoissant / attrs.jquery.js
Created January 31, 2017 23:04
Get all attributes of an element.
$.fn.attrs = function(){
var attributes = {};
if(this.length){
$.each( this[0].attributes, function( index, attr ) {
attributes[ attr.name ] = attr.value;
});
}
return attributes;
};
@dustinpoissant
dustinpoissant / cookies.js
Created November 27, 2019 20:09
Get, Set and Delete Cookies
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();
const stringToDOM = str => new DOMParser().parseFromString(str, 'text/html').body.childNodes[0];
@dustinpoissant
dustinpoissant / animateTransform.jquery.js
Last active July 2, 2018 20:46
jQuery Plugin to Animate Transforms
$.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 / $_GET.js
Last active February 14, 2018 20:30
JavaScript $_GET
var $_GET = (function(){
var params = {};
if(location.href.indexOf("?") > -1){
var string = location.href.split("?")[1];
if(string.indexOf("#") > -1){
string = string.split("#")[0];
}
var kvp = string.split("&");
for(var i=0; i<kvp.length; i++){
var k = kvp[i].split("=")[0];
@dustinpoissant
dustinpoissant / validColor.func.js
Last active December 20, 2017 09:41
A function to check if a string color is valid.
function validColor(color){
if(color=="")return false;
var $div = $("<div>");
$div.css("border", "1px solid "+color);
return ($div.css("border-color")!="")
}
@dustinpoissant
dustinpoissant / String.extract.func.js
Created December 1, 2017 17:38
Extract a substring from a string between two substrings.
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 / selectText.jquery.js
Created February 7, 2017 17:06
Selects the text within an element.
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();