Skip to content

Instantly share code, notes, and snippets.

View chrisjhoughton's full-sized avatar

Chris Houghton chrisjhoughton

  • Beacon
  • London, UK
View GitHub Profile
@chrisjhoughton
chrisjhoughton / centerPopup.js
Created March 15, 2014 21:37
Open a JavaScript popup window in the center of the screen. Kudos to http://stackoverflow.com/questions/4068373/center-a-popup-window-on-screen
var popupwindow = function (url, title, w, h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
};
@chrisjhoughton
chrisjhoughton / crop-resize.js
Last active January 9, 2021 05:19
Given an image URL or path, crop and resize it to be exactly a specific size. Crops centrally to force enforce the correct aspect ratio, and then resizes as per normal. Depends on the `when` and `gm` NPM modules. Returns a promise that resolves with an image buffer in a .PNG format.
/*
* Given an image URL or path, crop and resize it to be exactly a specific size.
* Crops centrally to force enforce the correct aspect ratio, and then resizes as per normal.
* Depends on the `when` and `gm` NPM modules.
* Returns a promise that resolves with an image buffer in a .PNG format.
*/
var when = require('when');
var gm = require('gm');
var im = gm.subClass({ imageMagick: true }); // use `im` in place of `gm` for heroku compatibility
@chrisjhoughton
chrisjhoughton / badbrowser.js
Created May 9, 2013 13:16
Show a message to people in IE8 or lower asking them to upgrade their browser. Depends on jQuery.
(function($) {
var BadBrowser = {
show: function() {
var _this = this;
if (_this.isIE8orLower()) {
_this.showMessage();
}
},
@chrisjhoughton
chrisjhoughton / bootstrap.js
Created August 14, 2014 10:56
SailsJS Geo-encoding and lookup
module.exports.bootstrap = function (cb) {
// Ensure we have 2dsphere index on Property so GeoSpatial queries can work!
sails.models.YOURMODEL.native(function (err, collection) {
collection.ensureIndex({ coordinates: '2dsphere' }, function () {
// It's very important to trigger this callack method when you are finished
// with the bootstrap! (otherwise your server will never lift, since it's waiting on the bootstrap)
cb();
@chrisjhoughton
chrisjhoughton / post-receive
Created January 27, 2014 11:53
Git automated deployments. Add this file to the /hooks directory, after a git init --bare is created. Note you'll also need to: 1) run chmod +x hooks/post-receive to ensure it's executable, and 2) create the target directory.
#!/bin/sh
GIT_WORK_TREE=/home/myuser/public_html/wp-content/themes/mytheme git checkout -f
@chrisjhoughton
chrisjhoughton / snippet.js
Last active January 2, 2016 07:39
The Sauce JavaScript snippet.
window.Sauce = {
isReady: false,
options: {},
object: {},
describe: function (type, data) {
this.object[type] = data;
@chrisjhoughton
chrisjhoughton / keenio-iframe.js
Created December 18, 2013 14:13
Load Keen.io JavaScript data collection SDK in a local iframe. The primary use-case for this is if you're tracking across multiple client sites and do not wish to pollute global name-spacing.
// Sample options to pass to Keen.configure
// {
// projectId: "your_project_id",
// writeKey: "your_write_key",
// }
var loadKeen = function (options, cb) {
var iframe = document.createElement("iframe");
iframe.style.display = "none";
@chrisjhoughton
chrisjhoughton / cookie.js
Last active December 30, 2015 21:50
Generic cookie functions
@chrisjhoughton
chrisjhoughton / indexOf.js
Created December 10, 2013 13:07
Cross-browser compatible indexOf
var indexOf = function(arr, item, fromIndex) {
fromIndex = fromIndex || 0;
if (arr === null) {
return -1;
}
var len = arr.length,
i = fromIndex < 0 ? len + fromIndex : fromIndex;
while (i < len) {
// we iterate over sparse items since there is no way to make it
@chrisjhoughton
chrisjhoughton / average.js
Created December 10, 2013 13:05
Very basic max, min, average functions. All depend on forEach: https://gist.github.com/chrisjhoughton/7890274
var average = function (arr) {
var total = 0;
forEach(arr, function (number) {
total += number;
});
return total / arr.length;
};