This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var encodeHTML = function(string) { | |
var tempDiv = document.createElement("div"); | |
tempDiv.innerText = string; | |
return tempDiv.innerHTML; | |
} | |
/* | |
encodeHTML("<em>Waffles</em>") | |
==> "<em>Waffles</em>" | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using simpleJSONP.js | |
// https://gist.github.com/z-------------/7389e7708a38021cd25e | |
/* output looks like this (just an example, not actual output) | |
data: { | |
status: 200, | |
weather: { | |
currentWarnings: [{ | |
title: "Red Rainstorm Signal", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lucky Duck chords/tabs | |
by Zachary Guard | |
B D A A | |
D |-----------------|-----4-----4-----|-----------------|-----------------| | |
A |-----5-----5-----|-5-5---5-5---5-5-|-----4-----7-----|-4-----7-----4---| | |
E |-7-7---7-7---7-7-|-----------------|-5-5---5-5---5-5-|---5-5---5-5---5-| | |
B D A E |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.search = function(string){ | |
var matches = []; | |
for (i=0; i<this.length; i++) { | |
if (this[i].indexOf(string) != -1) { | |
matches.push(this[i]); | |
} | |
} | |
return matches; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var header = document.querySelector("header"); | |
var headerHeight = header.offsetHeight; | |
var oldScroll = 0; | |
function hideHeader() { | |
header.style.top = -headerHeight + "px"; | |
} | |
function showHeader() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HTMLElement.prototype.center = function(){ | |
this.style.top = "50%"; | |
this.style.left = "50%"; | |
this.style.marginTop = -this.offsetHeight / 2 + "px"; | |
this.style.marginLeft = -this.offsetWidth / 2 + "px"; | |
} | |
// document.querySelector("#myDiv").center(); | |
// assumes position absolute or fixed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jsonp = function(url,callback) { | |
var callbackName = "jsonpCallback"+Math.round(Math.random()*10000000); // make (pretty) sure not to overwrite anything | |
window[callbackName] = callback; | |
var scriptElem = document.createElement("script"); | |
if (url.indexOf("?") != -1) { // url has "?" in it | |
scriptElem.src = url + "&callback=" + callbackName; | |
} else { | |
scriptElem.src = url + "?callback=" + callbackName; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var xhr = function(url, callback) { | |
var req = new XMLHttpRequest(); | |
req.onload = function() { | |
var response = this.responseText; | |
callback(response); | |
}; | |
req.open("get", url, true); | |
req.send(); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// recursive | |
var factorial = function(n) { | |
if (n == 1) { | |
return 1; | |
} else { | |
return n * factorial(n-1); | |
} | |
} | |
// iterative |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mouse = {}; | |
mouse.coords = {x:-1,y:-1}; | |
window.addEventListener("mousemove",function(e){ | |
mouse.coords.x = e.clientX; | |
mouse.coords.y = e.clientY; | |
}); | |
/* example: | |
if (mouse.coords.x > window.innerWidth / 2) { |