Skip to content

Instantly share code, notes, and snippets.

View esson's full-sized avatar

Martin Eriksson esson

  • byBrick
  • Stockholm
View GitHub Profile
@esson
esson / jquery.radioDefault.js
Last active December 14, 2015 14:08
jQuery Plugin. Set each radio button group to the first value.
/*
* jquery.radioDefault.js
* Sets the first radio button of each radio button group to checked.
*
* Version: 1.0
* Author: Martin Eriksson
*/
(function ($) {
@esson
esson / jquery.reloadCss.js
Last active December 15, 2015 10:18
This little snippet reloads all external stylesheets of a document. Perfect as a bookmarklet.
//javascript:
(function($){
function css() {
$('link[rel=stylesheet]').each(function(){
var $link=$(this);
var i=$link.attr('href').indexOf('?');
$link.clone().attr('href', $link.attr('href').substr(0, i > 0 ? i : undefined) + ('?' + Math.random()).replace('.', '')).load(function(){
$link.remove();
}).appendTo('head');
// Array Polyfills
// ---------------
(function () {
if (!Array.prototype.indexOf) {
// Production steps of ECMA-262, Edition 5, 15.4.4.14
// Reference: http://es5.github.com/#x15.4.4.14
@esson
esson / jQuery.resize.events.js
Last active December 22, 2015 04:28
Trigger events for 'resizestart' and 'resizeend' on $(window).
/**
* jQuery.resize.events.js
*
* Trigger events for 'resizestart' and 'resizeend' on $(window).
* @param {Number} timeoutms Defines the time in milliseconds before the 'resizeend' event is triggered.
*
* Version: 1.0
* Author: Martin Eriksson
*/
(function ($, timeoutms) {
@esson
esson / aliased.canvas.js
Last active December 23, 2015 03:59
Moves all x and y coordinates so that anti-aliasing will be kept to a minimum. This solves the problem where single pixel lines becomes 'blurry' by making sure that they are drawn at right position.
// Canvas Rendering Fix
//
if (window.CanvasRenderingContext2D) {
var arc = CanvasRenderingContext2D.prototype.arc,
arcTo = CanvasRenderingContext2D.prototype.arcTo,
bezierCurveTo = CanvasRenderingContext2D.prototype.bezierCurveTo,
clearRect = CanvasRenderingContext2D.prototype.clearRect,
fillRect = CanvasRenderingContext2D.prototype.fillRect,
lineTo = CanvasRenderingContext2D.prototype.lineTo,
@esson
esson / Format JSON Bookmarklet
Created October 30, 2014 12:44
Bookmarklet that formats the JSON content on a page.
javascript:document.write('<pre>'+JSON.stringify(JSON.parse(document.body.textContent),null,'\t')+'</pre>')
@esson
esson / SwedishPersonalIdentityNumber.cs
Last active August 29, 2015 14:19
Class for dealing with Swedish personal identity numbers.
public class SwedishPersonalIdentityNumber
{
/// <summary>
/// Pattern for finding common ways of writing a Swedish personal identity number. It is forgiving and will match the format rather than valid numbers.
///
/// Illustrating how it works:
/// ((19)99)(06)(01)(-)((01)|(TF))(1)(1)
/// </summary>
private const string RegexPattern = @"^(?<year>((19)|(20))?(\d\d))(?<month>[01]\d)(?<day>\d\d)(?<flag>[-]|[\+])?(?<county>(\d\d)|(TF))(?<gender>\d)(?<checksum>\d)$";
@esson
esson / getAspNetFormKeyValues.ts
Last active January 22, 2016 11:08
Returns a dictionary object with keys for each value in the provided object and its descendants/children. Keys are in the style of ASP.NET, i.e. "propertyName.collection[3]".
module AspNetUtilities {
export interface IAspNetFormValueDictionary {
[key: string]: any;
}
function collectAspNetFormKeyValues(keyPrefix: string, source: Object, dictionary: IAspNetFormValueDictionary) {
var localKeyPrefix = '';
(function () {
/**
* Preloader for Font Families. This will preload fonts used with CSS @font-face.
* @param fontFamilies {string[]} A collection of font family names to preload.
*/
var preloadFontFamilies = function (fontFamilies) {
// Create a hidden container.
//
var containerElement = document.createElement('div');
@esson
esson / jquery.dynamicForm.js
Created May 28, 2020 19:34
Add and remove inputs for form arrays.
(function ($) {
$.fn.dynamicForm = function (options) {
var config = $.extend({}, $.fn.dynamicForm.defaults, options),
$this = $(this),
$inputs = $this.find(config.container),
$add = $this.find(config.addButton),
$remove = $this.find(config.removeButton),
counter = $inputs.children().length;
$add.on('click', function (e) {