Skip to content

Instantly share code, notes, and snippets.

View phette23's full-sized avatar
🌹
"you're right, no human being would stack books like this"

Eric Phetteplace phette23

🌹
"you're right, no human being would stack books like this"
View GitHub Profile
@phette23
phette23 / bookmarkletTemplate.js
Created January 28, 2012 04:03
JavaScript Bookmarklet Template
//self-executing anonymous function
(function (window, document, undefined) {
var d=document; w=window; //aliases to save bytes
//do something
}(window, document));
//remove comments, put in a bookmark preceded by "javascript:"
@phette23
phette23 / jquery-plugin-tpl.js
Created January 30, 2012 22:22
jQuery Plugin Template
(function($){
$.fn.myPluginMethod = function() {
// keep it chainable, jQuery objects
// include multiple DOM elements
return this.each(function(){
// do something
});
};
}(jQuery));
@phette23
phette23 / integerBetween.js
Created February 3, 2012 00:10
Generate a random integer between any two values
var integerBetween = function(x, y) {
if ( typeof x !== "number" || typeof y !== "number") {
console.log('integerBetween cannot accept NaN values');
return false;
}
else {
var lowerBound = Math.floor(x);
var upperBound = Math.floor(y);
return Math.floor(Math.random() * (upperBound - lowerBound + 1) + lowerBound);
};
@phette23
phette23 / redirect.html
Created February 3, 2012 00:34
Screen-size based redirect
<!-- redirect based on screen width -->
<script type="text/javascript">
console.log('Screen width: '+screen.width);
if (screen.width <= 699 && !sessionStorage.getItem("redirecting")) {
window.location.replace("./m/index.html"); //path to your mobile site
}
</script>
<!-- redirect bypass on the mobile site
such that users don't get caught in a redirect loop
uses jQuery cuz mobile site is jQuery Mobile
@phette23
phette23 / lfm-recent-tracks.html
Created February 19, 2012 22:32
Last.fm Recent Tracks API
<h3>Last.fm Scrobbles</h3>
<!-- div below will be filled in with formatted contents of API response -->
<div id="recent-tracks" style="display:none;"><a href="http://last.fm/">Last.fm</a> data hasn't loaded yet.</div>
<!-- leave as display:none for the fade in effect -->
@phette23
phette23 / jquery-plugin-tpl-2.js
Created February 21, 2012 23:54
The Mike Alsup jQuery Plugin Development Pattern
//
//taken verbatim from jQuery Fundamentals
//which is at http://jqfundamentals.com/ (see chap. 8)
//and has an unmaintained github repo at https://github.com/rmurphey/jqfundamentals
//
// create closure
//
(function($) {
//
// plugin definition
@phette23
phette23 / cookies.js
Created March 22, 2012 16:18
JavaScript Functions for dealing with Cookies
//taken verbatim from the ever-vigilant Quirks Mode
//www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
@phette23
phette23 / modal-dialog.css
Created March 26, 2012 23:19
Modal Dialog Box
/* puts a gray overlay over the entire page
just turn on display when dialog pops up */
.modal-overlay {
display: none;
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
background-color: rgba(0,0,0,0.35);
}
@phette23
phette23 / trim.js
Created April 8, 2012 18:04
Crockford's String.trim() function
if (typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(
/^\s*(\S*(?:\s+\S+)*)\s*$/,
"$1");
};
}
@phette23
phette23 / sword.html
Created April 21, 2012 20:24
How to Make a Sword in HTML & CSS (no images)
<style>
#dagger {
/* important to have a serif font for the dagger
sans-serif will look like a cross
see: http://en.wikipedia.org/wiki/File:Daggers.svg */
font-family: "Old English Text MT", "Times New Roman", Times;
-moz-transform: rotate(180deg);
-webkit-transform: rotate(180deg);
-o-transform: rotate(180deg);
-ms-transform: rotate(180deg);