Skip to content

Instantly share code, notes, and snippets.

View mathiasbynens's full-sized avatar

Mathias Bynens mathiasbynens

View GitHub Profile
@mathiasbynens
mathiasbynens / Playing sounds on mouseover using jQuery
Created November 27, 2009 10:41
Playing sounds on mouseover using jQuery. Using <embed>, because who needs <audio> anyway‽
var $sound = $('<div id="sound" />').appendTo('body');
$('#special-links-that-play-annoying-sounds-when-hovered a').hover(function() {
$sound.html('<embed src="foo.mp3" hidden="true" autostart="true" loop="false">');
}, function() {
// We could empty the innerHTML of $sound here, but that would only slow things down.
});
@mathiasbynens
mathiasbynens / jquery.email-antispam.js
Created January 26, 2010 13:10
Simple spam protection for email addresses using jQuery
/* Simple spam protection for email addresses using jQuery.
* Well, the protection isn’t jQuery-based, but you get the idea.
* This snippet allows you to slightly ‘obfuscate’ email addresses to make it harder for spambots to harvest them, while still offering a readable address to your visitors.
* E.g.
* <a href="mailto:foo(at)example(dot)com">foo at example dot com</a>
* →
* <a href="mailto:foo@example.com">foo@example.com</a>
*/
$(function() {
@mathiasbynens
mathiasbynens / toggleAttr() jQuery plugin
Created February 8, 2010 21:20
toggleAttr() jQuery plugin
/*!
* toggleAttr() jQuery plugin
* @link http://github.com/mathiasbynens/toggleAttr-jQuery-Plugin
* @description Used to toggle selected="selected", disabled="disabled", checked="checked" etc…
* @author Mathias Bynens <http://mathiasbynens.be/>
*/
jQuery.fn.toggleAttr = function(attr) {
return this.each(function() {
var $this = $(this);
$this.attr(attr) ? $this.removeAttr(attr) : $this.attr(attr, attr);
@mathiasbynens
mathiasbynens / jquery.insertAtCaret.js
Created March 9, 2010 11:13
jQuery insertAtCaret plugin
// I found this somewhere on the intertubes, and optimized it
$.fn.insertAtCaret = function(myValue) {
return this.each(function() {
var me = this;
if (document.selection) { // IE
me.focus();
sel = document.selection.createRange();
sel.text = myValue;
me.focus();
} else if (me.selectionStart || me.selectionStart == '0') { // Real browsers
/*!
* JavaScript function to calculate the destination point given start point latitude / longitude (numeric degrees), bearing (numeric degrees) and distance (in m).
*
* Original scripts by Chris Veness
* Taken from http://movable-type.co.uk/scripts/latlong-vincenty-direct.html and optimized / cleaned up by Mathias Bynens <http://mathiasbynens.be/>
* Based on the Vincenty direct formula by T. Vincenty, “Direct and Inverse Solutions of Geodesics on the Ellipsoid with application of nested equations”, Survey Review, vol XXII no 176, 1975 <http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf>
*/
function toRad(n) {
return n * Math.PI / 180;
};
@mathiasbynens
mathiasbynens / jquery.crash-ie.js
Created April 8, 2010 11:53
jQuery Crash plugin — updated to work with IE6, IE7 and IE8
/*!
* jQuery Crash for IE6, IE7 and IE8
* @description This is an expansion of the jQuery Crash plugin for IE6 by Chad Smith.
* @author Mathias Bynens <http://mathiasbynens.be/>
* Use $.crashIE6(); and/or $.crashIE7(); and/or $.crashIE8();
*/
;
$.crashIE6 = function(x) {
if ($.browser.msie && $.browser.version < 7) {
for (x in document.open);
/*
* See http://mathiasbynens.be/notes/async-analytics-snippet#comment-14
*/
<script>
var f = Function(),
clicky = { log: f, goal: f },
clicky_site_id = XXXXXX;
(function(d, t) {
var c = d.createElement(t),
@mathiasbynens
mathiasbynens / jquery.preload.js
Created April 22, 2010 17:11
JavaScript preload() function
/*!
* $.preload() function for jQuery – http://mths.be/preload
* Preload images, CSS and JavaScript files without executing them
* Script by Stoyan Stefanov – http://www.phpied.com/preload-cssjavascript-without-execution/
* Slightly rewritten by Mathias Bynens – http://mathiasbynens.be/
* Note that since this script relies on jQuery, the preloading process will not start until jQuery has finished loading.
*/
jQuery.preload = function(array) {
var length = array.length,
@mathiasbynens
mathiasbynens / jquery.cookie.js
Created May 13, 2010 13:54
Improved jQuery.cookie plugin
@mathiasbynens
mathiasbynens / Minified version
Created June 5, 2010 09:38 — forked from paulirish/gist:426012
Optimized version of amelie.js for IE6
if(document.all&&!window.XMLHttpRequest){(function(){var b=1,a=0,e,c;function d(){if(b==2){b=0}if(!b++){e=c=0;a=~~(Math.random()*10000)+2000;setTimeout(d,a)}else{e=~~(Math.random()*2)+2;c=~~(Math.random()*360);setTimeout(d,500)}document.body.style.filter="progid:DXImageTransform.Microsoft.MotionBlur(strength="+e+",direction="+c+",enabled='true')"}setTimeout(d,1000)})()}