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 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] |
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
| console.log("%c%s", | |
| "color: red; background: yellow; font-size: 24px;", | |
| "WARNING!"); |
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 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 | |
| }); |
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
| 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); |
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
| 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; | |
| } | |
| } |
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
| / 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"] = {}; |
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
| 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]; |
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
| /* | |
| <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; |
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
| 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 |
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
| function nth( o ) { | |
| return o+(['st','nd','rd'][(o+'').match(/1?\d\b/)-1]||'th'); | |
| } |