Skip to content

Instantly share code, notes, and snippets.

View d33pfri3d's full-sized avatar
🏠
Working from home

Shaun D d33pfri3d

🏠
Working from home
View GitHub Profile
@d33pfri3d
d33pfri3d / EventSource
Created June 8, 2012 13:15
EventSource
var es = new EventSource('/console');
es.onmessage = function (event) {
var data = JSON.parse(event.data);
execute(data.command);
};
//custom message type
es.addEventListener('reload', function() {
reloadData();
@d33pfri3d
d33pfri3d / gist:2895625
Created June 8, 2012 13:30
Form data - Files > XHR
var formData = new FormData()
for (var i = 0; i < files.length; i++){
formData.append('file', files[i]);
}
//new post a new XHR request
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.upload.onprogress = function (event){
if (event.lengthComputable) {
@d33pfri3d
d33pfri3d / gist:2895628
Created June 8, 2012 13:31
preview D&D
function readFiles(files){
for (var i = 0; i < files.length; i++){
preview(files[i]);
}
}
function preview(file){
var reader = new FileReader();
reader.onload = function (event) {
var image = new Image();
@d33pfri3d
d33pfri3d / PrettyPrint Date
Created June 30, 2012 23:38
Date Function
function prettyDate(time){
var date = new Date(time || ""),
diff = ((new Date().getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) {
return;
}
return day_diff == 0 && (
var i = 0;
var numTouches = 0;
function touch( ev ) {
i++;
var out = document.getElementById('out');
if( ev.type == 'touchstart' ) {
out.value += i + ': touch start\n';
numTouches++;
}
@d33pfri3d
d33pfri3d / gist:3074506
Created July 9, 2012 06:20
Random IMGUR finder
function randomString(string_length)
{
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var output = "";
var index;
for (var i = 0; i < string_length; i++)
{
var index = Math.floor(Math.random() * chars.length);
output += chars.substring(index, index + 1);
@d33pfri3d
d33pfri3d / gist:3346618
Created August 14, 2012 05:33
Jquery Center Function
jQuery.fn.center = function() {
_TOP = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop();
_TOP = (_TOP <= 0) ? 0 : _TOP;
_LEFT= (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft();
_LEFT = (_LEFT <= 0) ? 0 : _LEFT;
this.css({
top:_TOP + "px",
left:_LEFT + "px"
});
return this;
//Conditionally load jQuery
//inspired by http://www.smashingmagazine.com/2010/05/23/make-your-own-bookmarklets-with-jquery/
window.onload = function () {
if (typeof jQuery == 'undefined') {
var jQ = document.createElement('script');
jQ.type = 'text/javascript';
jQ.onload = jQ.onreadystatechange = myOnLoadEvent;
jQ.src = ( "https:" == location.protocol ? "https//" : "http//" ) + 'ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
document.body.appendChild(jQ);
@d33pfri3d
d33pfri3d / statelist.html
Created August 25, 2012 19:44
Datalist of US States
<datalist id="statelist">
<option value="Alabama"></option>
<option value="Alaska"></option>
<option value="Arizona"></option>
<option value="Arkansas"></option>
<option value="California"></option>
<option value="Colorado"></option>
<option value="Connecticut"></option>
<option value="Delaware"></option>
<option value="District of Columbia"></option>
@d33pfri3d
d33pfri3d / placeholder.js
Created August 27, 2012 22:33
jQuery placeholder fallback
$(document).ready(function() {
if ( !("placeholder" in document.createElement("input")) ) {
$("input[placeholder]").each(function() {
var val = $(this).attr("placeholder");
if ( this.value == "" ) {
this.value = val;
}
$(this).focus(function() {
if ( this.value == val ) {