Skip to content

Instantly share code, notes, and snippets.

View garystorey's full-sized avatar
:octocat:

Gary Storey garystorey

:octocat:
View GitHub Profile
@garystorey
garystorey / mergeArray.js
Created July 8, 2015 20:51
Using an Array.prototype method, push in this case, allows you to merge two arrays. The alternative is iterating through the second array and using push on the first array. http://davidwalsh.name/merge-arrays-javascript
var array1 = [1, 2, 3];
var array2 = [4, 5, 6];
Array.prototype.push.apply(array1, array2);
console.log(array1); // is: [1, 2, 3, 4, 5, 6]
console.log("%c%s",
"color: red; background: yellow; font-size: 24px;",
"WARNING!");
var createProperty = function (obj, prop) {
var currentValue = obj[prop];
Object.defineProperty(obj, prop, {
get: function () { return currentValue; },
set: function (value) {
currentValue = value;
},
enumerable: true,
configurable: true
});
function uniques(array) {
var result = [], val, ridx;
outer:
for (var i = 0, length = array.length; i < length; i++) {
val = array[i];
ridx = result.length;
while (ridx--) {
if (val === result[ridx]) continue outer;
}
result.push(val);
localStorage.setItem('visited-'+window.location.pathname,true);
var links = document.getElementsByTagName('a');
for (i=0;i<links.length;i++) {
var link = links[i];
if (link.host == window.location.host
&& localStorage.getItem('visited-' + link.pathname + '/')) {
link.dataset.visited = true;
}
}
/ Changes XML to JSON
function xmlToJson(xml) {
// Create the return object
var obj = {};
if (xml.nodeType == 1) { // element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
function on(elSelector, eventName, selector, fn) {
var element = document.querySelector(elSelector);
element.addEventListener(eventName, function(event) {
var possibleTargets = element.querySelectorAll(selector);
var target = event.target;
for (var i = 0, l = possibleTargets.length; i < l; i++) {
var el = target;
var p = possibleTargets[i];
/*
<p>But haven't the sweetest idea <button>DELETE</button></p>
<p>To me buzz is <button>INSERT</button></p>
We'll use the two methods to do two simple things with this HTML: (1) Remove the word "idea" and (2) insert the word "onomatopoeia". Here's the JavaScript:
*/
var pars = document.querySelectorAll('p'),
btns = document.querySelectorAll('button'),
text;
@garystorey
garystorey / fadein.js
Created January 5, 2015 21:53
fadeIn.js
Element.prototype.fadeIn = function(callback) {
var ele = this,
opacity = ele.style.opacity = ele.style.opacity || 0.0,
fadeInLoop = null,
intervalTime = 10,
opacityAmount = 0.05;
ele.style.display = 'block';
// If the opacity is already greater than or equal to zero then there is nothing
function nth( o ) {
return o+(['st','nd','rd'][(o+'').match(/1?\d\b/)-1]||'th');
}