Skip to content

Instantly share code, notes, and snippets.

@devfred
devfred / async_load.js
Last active October 1, 2015 13:47
javascript: Load javascript resources asynchronously. Accepts additional parameters that can be passed to the specified callback function
/**
* Load javascript resources asynchronously. Accepts additional parameters that can be passed to the specified callback function
* Author: Frederick King
* Date: 3/8/2012
* References/Credits : http://css-tricks.com/thinking-async/ , http://mathiasbynens.be/notes/async-analytics-snippet
*
**/
var async_load_js = function( url, callback ){
var js, args, rs, s;
@devfred
devfred / localstorage.cookie.js
Last active October 1, 2015 13:58
javascript: Localstorage polyfill
@devfred
devfred / geolocation.js
Last active October 1, 2015 13:58 — forked from paulirish/gist:366184
javascript: html5 geolocation with fallback.
// geo-location shim
// currentely only serves lat/long
// depends on jQuery
;(function(geolocation){
if (geolocation) return;
var cache;
@devfred
devfred / blackandwhiteimages.php
Created April 14, 2012 13:57
php: Generate B + W images in WordPress
<?php
add_image_size('thumbnail-bw', 400, 0, false);
add_filter('wp_generate_attachment_metadata','bw_images_filter');
function bw_images_filter($meta) {
$file = wp_upload_dir();
$file = trailingslashit($file['path']).$meta['sizes']['thumbnail-bw']['file'];
list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
$image = wp_load_image($file);
imagefilter($image, IMG_FILTER_GRAYSCALE);
@devfred
devfred / ms-xdr.js
Created May 18, 2012 16:28
javascript: Cross Domain Request for IE8+
// Get weather for charlotte using YQL + Yahoo weather
var xdr = new XDomainRequest();
xdr.open("get", "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D'http%3A%2F%2Fxml.weather.yahoo.com%2Fforecastrss%2F28215_f.xml'&format=json");
xdr.onload = function() {
// the string now looks like.. json = { ... };
json = 'json = ' + xdr.responseText;
// json is now a regular JSON object
eval(json);
// parse using same function as for jQuery's success event
self.draw(json.query.results.item.forecast, $elem);
@devfred
devfred / jquery.center.js
Created May 18, 2012 16:36
javascript: Center element within its parent
$.fn.center = function () {
this.css("position","absolute");
this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
return this;
}
@devfred
devfred / globalEval.js
Created May 18, 2012 16:38
javascript: Evaluate a script in a global context
// Evaluates a script in a global context
// Workarounds based on findings by Jim Driscoll
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
// pulled from jquery.core source
var globalEval = function( data ){
if ( data ) {
// We use execScript on Internet Explorer
// We use an anonymous function so that context is window
// rather than jQuery in Firefox
@devfred
devfred / RequesQ.js
Last active March 7, 2016 23:18
javascript: Xhr with queue example
/* Queue to use in conjuction with xhr functionality */
var requestQ = (function() {
var numRequestToComplete, requestsCompleted, callBacks, singleCallBack;
return function(options) {
if (!options) options = {};
numRequestToComplete = options.numRequest || 0;
requestsCompleted = options.requestsCompleted || 0;
callBacks = [];
var fireCallbacks = function() {
/* alert("we're all complete"); */
@devfred
devfred / PreJqueryDomReady.js
Last active October 5, 2015 00:48
javascript: jQuery dom ready before jquery is loaded
/* This snippet allowes function to user jQuery dom ready before jquery is loaded */
window.$ = (function() {
var q = [], f = function (cb) {
q.push(cb);
};
f.attachReady = function ($) {
$(function () {
$.each(q, function(i, f) {
f();
});
@devfred
devfred / jquery.fk-qr.js
Last active December 21, 2023 10:31
javascript: Generate QR code on the fly
/**
Quick and dirty plugin to generate qr code on the fly
@author Frederick King
**/
(function($, undefined) {
/**
Generate qr code on the fly
@parameter {object} options
**/
$.fn.qrCode = function(options) {