Skip to content

Instantly share code, notes, and snippets.

@sang4lv
sang4lv / gist:4442539
Created January 3, 2013 10:45
Basic Ajax Review
function createRequest() {
var request = null;
try {
request = new XMLHttpRequest();
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.HTTPRequest");
} catch (otherMS) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
@sang4lv
sang4lv / gist:4442785
Created January 3, 2013 11:24
History API
window.history.back();
window.history.forward();
window.history.go(); //optionally give a number. -1 = back, 1 = forward
var allEntries = window.history.length; //entry length
var currentState = window.history.state;
history.pushState(, "Introduction to History API", "working.html");
history.replaceState(, "Briefs on History API", "current.html");
history.onpopstate = function() {
console.log("It has navigated to a different page");
@sang4lv
sang4lv / gist:4442930
Created January 3, 2013 11:45
Ajax Level 2: Handling BLOB Data
window.URL = window.URL || window.webkitURL; // Take care of vendor prefixes.
var xhr = new XMLHttpRequest();
xhr.open('GET', '/path/to/image.png', true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
var blob = this.response;
@sang4lv
sang4lv / gist:4442953
Created January 3, 2013 11:52
Ajax Level 2: Send Form Data
function sendForm(form) {
var formContent = new FormData(form); //Get existing form content. Optional argument
formContent.append("hiddenId", "1234");
formContent.append("iceCream", "Vanilla");
formContent.append("currentLocation", "1223084.284|1209843.239");
var progressBar = document.querySelector("progress");
var xhr = new XMLHTTPRequest();
xhr.open("POST", "server.php", true);
xhr.send(formContent);
@sang4lv
sang4lv / gist:4442964
Last active December 10, 2015 13:48
Ajax Level 2: Sending large files fast
function upload(blobOrFile) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/server', true);
xhr.onload = function(e) { console.log("Upload has finished."); };
xhr.send(blobOrFile);
}
document.querySelector('input[type="file"]').addEventListener('change', function(e) {
var blob = this.files[0];
@sang4lv
sang4lv / gist:4443484
Created January 3, 2013 13:32
Web Sockets: Introduction
var connection = new WebSocket('ws://html5rocks.websocket.org/echo', ['soap', 'xmpp']);
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};
connection.onmessage = function (e) {
console.log('Server: ' + e.data);
};
function sendData(type, content) {
@sang4lv
sang4lv / gist:4444696
Created January 3, 2013 16:26
IndexedDB: Add, Delete, and Get from Object Store
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
if(!window.indexedDB) {
console.log("IndexDB is not supported on your browser");
}
function reportError(event) {
console.log("Error with Database. " + event.target.errorCode);
}
@sang4lv
sang4lv / gist:4451534
Created January 4, 2013 10:26
Geolocation: Everything
function gotLocation(position) {
console.log("I am at latitude " + position.coords.latitude + ", longitude " + position.coords.longitude + ", and accurate within " + position.coords.accuracy + " meter(s).");
}
function handleLocationError(event) {
switch(event.errorCode) {
case 0:
console.log("We have an unknown error...");
break;
case 1:
@sang4lv
sang4lv / gist:4460459
Created January 5, 2013 08:06
Manifest File: Overview
CACHE MANIFEST
CACHE: #Explicitly Cached after first download
sample1.png #Use version number to notify update/modification on file
sample2.png
NETWORK: #Items that must be retrieved from the server, regardless of the connection
sample3.png
@sang4lv
sang4lv / gist:4460999
Created January 5, 2013 11:00
Server sent events (SSE): Overview
var serverEvent = new EventSource("http://example.com/eventsource.php"); //CORS is supported
serverEvent.onerror = function(event) {
console.log(event.target.errorCode);
};
serverEvent.onopen = function() {
console.log("Connection has been established");
};