Skip to content

Instantly share code, notes, and snippets.

View jonkemp's full-sized avatar

Jonathan Kemp jonkemp

View GitHub Profile
@jonkemp
jonkemp / templateManager.js
Last active June 8, 2016 08:09
Load and cache external templates with jQuery and Underscore. Returns a promise. Useful for JavaScript MVC frameworks, such as Backbone.js.
/* global $, _ */
var TemplateManager = {};
(function () {
'use strict';
var cache = {};
TemplateManager.template = function (path) {
@jonkemp
jonkemp / bookmarklets.html
Created January 30, 2017 04:14
Bookmarklets
<!-- switch to htts if available -->
<a href="javascript:(function(){if(location.protocol!='https:'){location.href='https:'+window.location.href.substring(window.location.protocol.length);}})();">HTTPS Switcher</a>
@jonkemp
jonkemp / .editorconfig
Last active April 11, 2017 17:20
gulp-jquery-t3 sample project
# editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
@jonkemp
jonkemp / timestamp.js
Created May 11, 2012 15:06
Print out a nicely formatted timestamp in JavaScript.
/**
* Return a timestamp with the format "m/d/yy h:MM:ss TT"
* @type {Date}
*/
function timeStamp() {
// Create a date object with the current time
var now = new Date();
// Create an array with the current month, day and time
@jonkemp
jonkemp / prepopulate.html
Created April 5, 2011 15:11
Pre-populate your forms with random data. For testing forms. Requires jQuery. Includes a bookmarklet.
<!-- include jQuery -->
<script>
$('form').find('input:text').val( function(i, val) {
return $(this).attr('name');
});
$('form').find('select').each( function(a) {
$(this).find('option').each( function(b) {
if ( $(this).val() !== '' ) {
$(this).parent().val( $(this).val() );
@jonkemp
jonkemp / css-calc.css
Created April 2, 2012 04:17
Cross Browser CSS Calc()
/* 1. write it out for older browsers */
/* 2. use the vendor prefix for webkit */
/* 3. use the vendor prefix for moz */
/* 4. include the un-prefixed version last */
#foo {
width: 200px;
width: -webkit-calc(50% - 100px);
width: -moz-calc(50% - 100px);
width: calc(50% - 100px);
@jonkemp
jonkemp / index.js
Created March 29, 2021 17:55
The Module Pattern from Learning JavaScript Design Patterns by Addy Osmani
const testModule = (() => {
let counter = 0;
return {
incrementCounter() {
return counter++;
},
@jonkemp
jonkemp / dev_server.md
Last active October 17, 2022 01:09
Starting a development server

Start a dev server to preview your work using Python, Ruby or Node.js

Requirements

Mac OS: Ruby and Python are pre-installed. Install Node.js.

Windows: First install Ruby, Python or Node.js

Ruby

@jonkemp
jonkemp / validate-currency.js
Last active June 8, 2023 17:29
Currency validation method for the jQuery Validation plugin. Decimal place is optional but if included, it requires 2 places. Also, the dollar sign is optional.
// Validation method for US currency
$.validator.addMethod("currency", function (value, element) {
return this.optional(element) || /^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/.test(value);
}, "Please specify a valid amount");