Skip to content

Instantly share code, notes, and snippets.

@z-------------
z------------- / encodeHTML.js
Created June 9, 2014 09:22
Encode a string with HTML entities
var encodeHTML = function(string) {
var tempDiv = document.createElement("div");
tempDiv.innerText = string;
return tempDiv.innerHTML;
}
/*
encodeHTML("<em>Waffles</em>")
==> "&lt;em&gt;Waffles&lt;/em&gt;"
*/
@z-------------
z------------- / simpleJSONP-example.js
Last active August 29, 2015 14:02
Example usage of simpleJSONP.js
// 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",
@z-------------
z------------- / lucky-duck-chords
Last active August 29, 2015 14:02
Lucky Duck chords/tabs
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
@z-------------
z------------- / arraySearch.js
Created May 31, 2014 08:51
Search a string array for a string and return matching strings (Read the code, it's self-explanatory)
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;
}
@z-------------
z------------- / afh.js
Created May 29, 2014 14:50
Awesome Floating Header - hide the header when scrolling down, show when scrolling up.
var header = document.querySelector("header");
var headerHeight = header.offsetHeight;
var oldScroll = 0;
function hideHeader() {
header.style.top = -headerHeight + "px";
}
function showHeader() {
@z-------------
z------------- / centerElement.js
Last active August 29, 2015 14:01
Position an element at the center of the screen
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
@z-------------
z------------- / simpleJSONP.js
Last active August 29, 2015 14:01
A simple way to use JSONP
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;
@z-------------
z------------- / simpleXHR.js
Last active August 29, 2015 14:01
A simple way to use XMLHttpRequest (XHR)
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();
};
@z-------------
z------------- / factRecursiveAndIterative.js
Created May 19, 2014 14:12
Two ways to calculate factorials: recursively and iteratively. http://jsperf.com/recursive-vs-iterative-factorial
// recursive
var factorial = function(n) {
if (n == 1) {
return 1;
} else {
return n * factorial(n-1);
}
}
// iterative
@z-------------
z------------- / easyMouseCoords.js
Last active August 29, 2015 14:01
Get mouse coordinates with mouse.coords object. The script handles the event for you.
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) {