Skip to content

Instantly share code, notes, and snippets.

@mbjordan
mbjordan / format-number.js
Created January 10, 2012 20:41
Format a number with commas in JavaScript
function number_format(n) {
n += '';
x = n.split('.');
y = x[0];
z = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(y)) {
y = y.replace(rgx, '$1' + ',' + '$2');
}
return y + z;
@mbjordan
mbjordan / jQuery.mintip.js
Created January 12, 2012 03:03 — forked from FND/jQuery.mintip.js
jQuery MinTip: minimal tooltips
/*jslint vars: true, unparam: true, browser: true, white: true */
/*global jQuery */
(function ($) {
"use strict";
var onEnter, onLeave, startTimer, stopTimer, remove, getPos;
var defaults = {
delay: 500,
content: null,
allowHTML: false // XXX: rename
};
@mbjordan
mbjordan / jquery.copylink.js
Created February 7, 2012 13:08
Simple jQuery function that changes an <a> element to an <input> for copying the href contents.
@mbjordan
mbjordan / sticky-on-scroll.css
Created February 14, 2012 13:07
Simple jQuery Sticky-on-Scroll Navigation
#wrapper { position: relative; }
/* Aside in non-scroll mode */
aside {
float: left; /* Keeps the aside to the left and on top */
margin-top: 5px;
padding: 15px 0 15px 8px;
width: 267px;
}
@mbjordan
mbjordan / texterize.php
Created March 3, 2012 16:40
Strip down and optimize a string for the Google Web Fonts "text" parameter.
<?php
function texterize($s = '') {
$s = str_replace(' ', '', $s);
$s = str_split($s);
$s = array_unique($s);
natcasesort($s);
return urlencode(implode($s));
}
@mbjordan
mbjordan / Bcrypt.php
Created June 7, 2012 12:02
PHP Bcrypt functions
<?php
/*
BCRYPT function group
*/
function bcrypt_hash($password, $work_factor = 8) {
if ($work_factor < 4 || $work_factor > 31) $work_factor = 8;
$salt = '$2a$'.str_pad($work_factor, 2, '0', STR_PAD_LEFT).'$'.
substr(
@mbjordan
mbjordan / next-last.sql
Created June 7, 2012 12:05
Get the next and last rows from SQL
-- The comparison "_CURRENT-ID_" is an integer.
--Last Row:
SELECT `field1`
FROM `tablename`
WHERE `id` < '_CURRENT-ID_'
ORDER BY `id` DESC
LIMIT 1
@mbjordan
mbjordan / styleNumber.js
Created November 13, 2012 19:42
Style a number
function styleNumber(number, precision) {
if (number.toString().length >= 4) {
var place = 1;
if (precision !== undefined && precision === 1) {
place = 10;
} else if (precision === 2) {
place = 100;
}
number = ((number / 100) / 10) * place;
number = Math.round(number) / place + ' k';
@mbjordan
mbjordan / ref-mask.php
Created November 19, 2012 13:06
HTTP Referral Masking
<?php
session_start();
/**
Setp 1. Get the query string variable and set it in a session, then remove it from the URL.
*/
if (isset($_GET['to']) && !isset($_SESSION['to'])) {
$_SESSION['to'] = urldecode($_GET['to']);
header('Location: http://yoursite.com/path/to/ref-mask.php');// Must be THIS script
exit();
@mbjordan
mbjordan / mobileNav.js
Created November 19, 2012 15:01
mobileNav.js, an alternative to tinyNav.js without markup assumption
/*
mobileNav.js by Matt Jordan.
*/
(function ($) {
$.fn.mobileNav = function (mobileWidth) {
// Create the <select> element
var selectHTML = '<select name="mobileNav" id="mobileNav" style="display:none">',
currentHref = window.location.href,
checkSize = function () {