Skip to content

Instantly share code, notes, and snippets.

View kn9ts's full-sized avatar
🌠
I am one with the force!

Eugene Mutai kn9ts

🌠
I am one with the force!
View GitHub Profile
@kn9ts
kn9ts / ObserverDP.html
Created April 28, 2015 08:49
Creating an observer with an observable pattern
<html>
<head>
<script language="javascript">
function ArrayList() {
this.aList = []; //initialize with an empty array
}
ArrayList.prototype.Count = function() {
return this.aList.length;
@kn9ts
kn9ts / XHR.js
Created May 4, 2015 10:01
How to Make AJAX Requests With Raw Javascript
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = isReady(function(xhr) {
console.log(xhr.responseText);
});
function isReady(callback) {
if (xhr.readyState < 4) {
return;
}
// ==UserScript==
// @name Use Markdown, sometimes, in your HTML.
// @author Paul Irish <http://paulirish.com/>
// @link http://git.io/data-markdown
// @match *
// ==/UserScript==
// If you're not using this as a userscript just delete from this line up. It's cool, homey.
@kn9ts
kn9ts / XMLHttpRequest.js
Created May 7, 2015 07:07
Making an ajax request using plain JavaScript
var data = {
ids: [12, 18, 27, 35, 41, 53, 66, 68, 72, 85, 94, 103, 111, 120, 133]
}
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(xhr) {
if (xhr.readyState < 4 || xhr.status !== 200) {
return;
}
@kn9ts
kn9ts / interpolate.js
Last active August 29, 2015 14:20 — forked from cowboy/interpolate.js
// Uber-basic templating.. good? bad? ugly?
function interpolate( str, data ) {
data = data || window;
return !str
? ''
: str.replace( /{([^}]*)}/g, function(a,b){
return data[ b ] || '';
});
};
@kn9ts
kn9ts / tim.js
Last active August 29, 2015 14:20 — forked from bruntonspall/tim.js
/*
== Tim ==
A tiny JavaScript micro-templating script.
http://gist.github.com/521352
You can use Tim to write simple templates that use JavaScript's
familiar dot notation to replace template tags with JSON object
properties.
@kn9ts
kn9ts / ajax-request-long.js
Created May 11, 2015 06:24
Submiting a form using ajax, the somehow long way
// get the form
var theForm = document.getElementById("vitumob-user-form");
var formdata = new FormData(theForm);
//Add the HTML file for the site that failed to be filtered
formdata.append("part_html", status.html);
// Though: why not also some important data
formdata.append("related_url", document.URL);
formdata.append("related_host", document.location.host)
@kn9ts
kn9ts / localStorage-mini.js
Created May 13, 2015 08:25
Simple local storage function to store data in local storage and cookies if jquery plugin is existent
/**
* LOCAL STORAGE MANAGEMENT FUNCTION
* @param options - local(bool), content(object), backup(bool)
* @param key
* STORE CONTENT locally or in cookie or BOTH
*
* HOW TO USE:
localStorage('key') //Returns the content if existing, or false if it doesnt
localStorage('key', {
content: the content, can be a raw object, string or raw array //it is stringified by the function
@kn9ts
kn9ts / capitalize.string.js
Created May 13, 2015 18:48
Function to capitalise the 1st letters in a string
// FUNCTION TO CAPITALIZE THE !ST LETTERS IN A STRING
String.prototype.capitalize = function() {
return this.replace(/(^|\s)([a-z])/g, function(m, p1, p2) {
return p1 + p2.toUpperCase();
});
};
@kn9ts
kn9ts / text-child-nodes.js
Created May 14, 2015 07:30
Looping through child nodes/element siblings inclusive of textual content looking for text
Array.prototype.filter.call(e.querySelector("div").childNodes, function(e) {
return 3 == e.nodeType && -1 != e.nodeValue.indexOf(":")
}).forEach(function(e) {
e = e.nodeValue.split(":", 2);
t[e[0].trim().toLowerCase()] = e[1].trim()
});