Skip to content

Instantly share code, notes, and snippets.

View dustinpoissant's full-sized avatar

Dustin Poissant dustinpoissant

View GitHub Profile
@dustinpoissant
dustinpoissant / saveAs.func.js
Last active September 13, 2016 14:03
Saves text in a text file
function saveAs(text, filename){
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=urf-8,'+encodeURIComponent(text));
pom.setAttribute('download', filename);
pom.click();
};
@dustinpoissant
dustinpoissant / toFunc.func.js
Created September 13, 2016 14:02
A JavaScript function that converts anything into a function.
function toFunc(v){
if(typeof(v) == "function") return v;
if(typeof(v) == "string"){
if(
window[v] != undefined &&
typeof(window[v]) == "function"
){
return window[v];
}
try {
@dustinpoissant
dustinpoissant / formatNumber.func.js
Last active January 25, 2017 21:33
A JavaScript function to format a number into a string.
function formatNumber(v, format){
if(format.indexOf(".")>-1){
var neg = false;
if(v<0)neg = true;
v = Math.abs(v);
var bd = format.indexOf(".");
var ad = format.length-bd-1;
var b = Math.floor(v);
var a = v - b;
b+="";
@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 / Math.randInt.js
Created November 8, 2016 19:46
Generate a Random Integer within a range.
Math.__proto__.randInt = function(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
};
@dustinpoissant
dustinpoissant / String.replaceAll.js
Created November 11, 2016 15:36
Replace all occurrences in a string
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
@dustinpoissant
dustinpoissant / getScrollbarWidth.js
Last active November 19, 2016 21:22
Get Scrollbar Width
function getScrollbarWidth () {
var inner = document.createElement('p');
inner.style.width = "100%";
inner.style.height = "200px";
var outer = document.createElement('div');
outer.style.position = "absolute";
outer.style.top = "0px";
outer.style.left = "0px";
outer.style.visibility = "hidden";
outer.style.width = "200px";
@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 / 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")!="")
}