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 / getDate.js
Created January 31, 2014 15:53
Generic jQuery-based scraping functions created by the engineering team @QuBiT
// Get a date
// Requires getString and isValidDate
var getDate = function (selectorOrEl) {
// Get text
var text = getString(selectorOrEl);
// Try and get the date, return text failing that
var date = new Date(text);
if (this.isValidDate(date)) {
@chrisjhoughton
chrisjhoughton / append-style.js
Created February 18, 2014 14:20
Add CSS styles to the <head> using jQuery
var styleText = "body { background: black; }";
$( "<style>" + styleText + "</style>" ).appendTo( "head" );
@chrisjhoughton
chrisjhoughton / create-el.js
Created February 18, 2014 14:26
Create a div element with Jquery
jQuery('<div/>', {
id: 'foo',
href: 'http://google.com',
title: 'Become a Googler',
rel: 'external',
text: 'Go to Google!'
}).appendTo('#mySelector');
@chrisjhoughton
chrisjhoughton / multiline.js
Created March 6, 2014 13:47
DO NOT USE. CURRENTLY BUGGY. An extremely useful yet nasty function to pass native HTML into a JavaScript function. Compatible with IE8+
var multiline = function (fn) {
var reCommentContents = /\/\*\s*([\s\S]*?)\s*\*\//;
return reCommentContents.exec(fn.toString())[1];
};
var html = multiline(function(){/*
<div>
<marquee>HELLO</marquee>
</div>
*/});
@chrisjhoughton
chrisjhoughton / contains.js
Created March 13, 2014 13:45
Check if a value exists in an array.
var contains = function(arr, value) {
var i = arr.length;
while (i--) {
if (arr[i] === value) {
return true;
}
}
return false;
};
@chrisjhoughton
chrisjhoughton / whenPopupClosed.js
Created March 15, 2014 09:29
Detect when a popup has closed on the page using polling. Kudos to http://stackoverflow.com/questions/3291712/is-it-possible-to-open-a-popup-with-javascript-and-then-detect-when-the-user-clo for the idea. Polling isn't neat, but there's not a neater way here.
var whenPopupClosed = function (popupWindow, cb) {
var pollTimer = window.setInterval(function() {
if (popupWindow.closed !== false) { // !== is required for compatibility with Opera
window.clearInterval(pollTimer);
cb();
}
}, 200);
};
@chrisjhoughton
chrisjhoughton / getAvatar.js
Created August 7, 2014 16:58
Get an avatar by a user's email address. Relies on Mout and MD5
var md5 = require("MD5");
var trim = require("mout/string/trim");
module.exports = function (email) {
var hash = md5(trim(email).toLowerCase());
return "http://www.gravatar.com/avatar/" + hash;
};
@chrisjhoughton
chrisjhoughton / video_embed_code.js
Created September 22, 2014 13:45
Get the video embed code from either a Vimeo or Youtube URL. Depends on Mout (moutjs.com)
var getParam = require("mout/queryString/getParam");
module.exports = function (url) {
if (!url) {
return "";
}
// Youtube
if (url.indexOf("youtube.com") !== -1) {
@chrisjhoughton
chrisjhoughton / sauce-identify.html
Last active August 29, 2015 14:26
Pass a user's email address to Sauce, after an email subscribe form has been submitted. Requires jQuery.
<script>
/*
* Key things to change:
* 1. The class `form.my-subscribe-form` to match your own
* 2. The input `input[name='email']` to match the real `name` of the input
*/
$(function () {
$('form.my-subscribe-form').on('submit', function () {
var email = $(this).find("input[name='email']").val();
@chrisjhoughton
chrisjhoughton / contains.js
Last active December 17, 2015 04:59
See if an array/string contains something. Nothing fancy, but without a library you'll need this as array.indexOf doesn't work in IE8 and lower.
var contains = function (list, value) {
if (list instanceof Array) {
var i = list.length;
while (i--) {
if (list[i] === value) {
return true;
}
}
return false;
} else if (typeof list === "string") {