Skip to content

Instantly share code, notes, and snippets.

View sindresorhus's full-sized avatar
🌴
On vacation

Sindre Sorhus sindresorhus

🌴
On vacation
View GitHub Profile
@sindresorhus
sindresorhus / jquery.toggletext.js
Created August 12, 2010 14:39
jQuery plugin - Toggle between the current and supplied text
$.fn.toggleText = function(b) {
return this.each(function() {
var $this = $(this);
if ( !$this.data('text') ) {
$this.data( 'text', $this.text() );
}
$this.text( $this.text() !== b ? b : $this.data('text') );
});
};
@sindresorhus
sindresorhus / jquery.hoverclasslive.js
Created August 13, 2010 09:28
jQuery plugin - Add hover in IE6 on live elements
$.fn.hoverClassLive = function(str) {
var klass = str || 'hover';
return this.live('hover', function() {
$(this).toggleClass(klass);
});
};
@sindresorhus
sindresorhus / jquery.random.js
Created September 24, 2010 18:32
jQuery plugin - Returns a random element
$.fn.random = function() {
var rand = Math.floor( Math.random() * this.length + 1 );
return this[rand];
};
@sindresorhus
sindresorhus / mysql-backup-windows.bat
Created March 14, 2011 14:50
Backup MySQL databases in separate gzipped sql files on Windows
@echo off
set dbUser=root
set dbPassword=password
set backupDir="C:\Documents and Settings\user\Desktop\backup\mysql"
set mysqldump="C:\Program Files\MySQL\MySQL Workbench 5.2 CE\mysqldump.exe"
set mysqlDataDir="C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\data"
set zip="C:\Program Files\7-Zip\7z.exe"
:: get date
@sindresorhus
sindresorhus / jquery.samesize.js
Created April 24, 2011 14:10
jQuery plugin - Equalize the elements to the same size - width or height.
$.fn.sameSize = function( width, max ) {
var prop = width ? 'width' : 'height',
size = Math.max.apply( null, $.map( this, function( elem ) {
return $( elem )[ prop ]();
})),
max = size < max ? size : max;
return this[ prop ]( max || size );
};
// same height
@sindresorhus
sindresorhus / init.py
Created August 28, 2011 18:09
Boxee - Picture player
import mc
mc.ActivateWindow(14000)
item_list = mc.ListItems()
item = mc.ListItem(mc.ListItem.MEDIA_PICTURE)
item.SetPath('http://sindresorhus.com/slider-img/1.jpg')
item.SetImage(0, 'http://sindresorhus.com/slider-img/1.jpg')
item.SetContentType('image/jpeg')
@sindresorhus
sindresorhus / jquery.touchhover.js
Created October 11, 2011 12:58
jQuery plugin - Adds a .hover class on click on touch devices. Which will make it easier to use :hover and still support touch devices.
$.fn.touchHover = function() {
return 'ontouchstart' in document.documentElement ? this.click(function() {
$(this).toggleClass('hover');
}) : this;
};
/*
Example: $('.button').touchHover();
You also need to add .hover to your :hover CSS rules:
.button:hover -> .button:hover, .button.hover
*/
@sindresorhus
sindresorhus / change_form.html
Created October 14, 2011 12:10
Django - If you have 2 date-pickers in the admin form, one for start date and one for end date, this will make sure the second picker is the same month as the first. Which improves the usability of the picker.
{% extends "admin/change_form.html" %}
{% block extrahead %}
{{ block.super }}
<script >
// override openCalendar to set the date of calendar A to calendar B
var _openCalendar = DateTimeShortcuts.openCalendar;
DateTimeShortcuts.openCalendar = function(num) {
_openCalendar.call( this, num );
var cals = DateTimeShortcuts.calendars;
cals[1].drawDate( cals[0].currentMonth, cals[0].currentYear );
@sindresorhus
sindresorhus / countrycode-latlong-array.json
Created November 5, 2011 16:03
Country codes (ISO 3166) to latitude longitude - converted from http://www.maxmind.com/app/country_latlon
{
"ad": [
"42.5000",
"1.5000"
],
"ae": [
"24.0000",
"54.0000"
],
"af": [
@sindresorhus
sindresorhus / simplexhr.js
Created January 9, 2012 15:19
Simple XMLHttpRequest wrapper
var xhr = function() {
var xhr = new XMLHttpRequest();
return function( method, url, callback ) {
xhr.onreadystatechange = function() {
if ( xhr.readyState === 4 ) {
callback( xhr.responseText );
}
};
xhr.open( method, url );
xhr.send();