Skip to content

Instantly share code, notes, and snippets.

@ahsquared
ahsquared / defer_doc_ready_jquery.html
Created April 26, 2012 05:51
Defer a document ready call
<!DOCTYPE html>
<html lang="en">
<head>
<!-- this collects the jquery document ready calls
// used with gratitude from: http://blog.colin-gourlay.com/blog/2012/02/safely-using-ready-before-including-jquery/ -->
<script>(function (w, d, u) { w.readyQ = []; w.bindReadyQ = []; function p(x, y) { if (x == "ready") { w.bindReadyQ.push(y); } else { w.readyQ.push(x); } }; var a = { ready: p, bind: p }; w.$ = w.jQuery = function (f) { if (f === d || f === u) { return a } else { p(f) } } })(window, document)</script>
</head>
<body>
<!-- this is on the page and you want to defer it -->
<script type="text/javascript">
@ahsquared
ahsquared / scrollable_multiple_items_displayed_fix
Created December 14, 2012 02:31
jQuery tools scrollable doesn't handle scrolling one at a time but showing multiples, this fixes that.
$('#divMonthsOuter .scrollable').scrollable({
circular: false,
onSeek: function (event, index) {
var next = this.getRoot().siblings('.next');
// the 6 here is the number of items showing
if (this.getIndex() >= this.getSize() - 6) {
// Disable the Next link
next.addClass('disabled');
} else {
next.removeClass('disabled');
@ahsquared
ahsquared / Detect Document Width and Height
Last active December 15, 2015 23:49
js to accurately find width and height of document
function docElem(property) {
var t;
return ((t = document.documentElement) || (t = document.body.parentNode)) && t[property] ? t : document.body;
}
// View width and height excluding any visible scrollbars
// http://www.highdots.com/forums/javascript/faq-topic-how-do-i-296669.html
// 1) document.client[Width|Height] always reliable when available, including Safari2
// 2) document.documentElement.client[Width|Height] reliable in standards mode DOCTYPE, except for Safari2, Opera<9.5
// 3) document.body.client[Width|Height] is gives correct result when #2 does not, except for Safari2
; Sources:
; http://reference.sitepoint.com/css
; http://developer.mozilla.org/en/CSS
animation = "none"
animation-delay = "0"
animation-direction = "normal"
animation-duration = "0"
animation-fill-mode = "none"
animation-iteration-count = "1"
@ahsquared
ahsquared / format-currency.js
Created June 27, 2013 05:36
Format currency putting in commas every 3 digits and a $ sign at the front. Input a number, returns a string.
function formatCurrency (num) {
aDigits = num.toFixed(2).split(".");
aDigits[0] = aDigits[0].split("").reverse().join("").replace(/(\d{3})(?=\d)/g, "$1,").split("").reverse().join("");
return "$" + aDigits.join(".");
}
TEST RESULTS
OS Supported?
Android 1.0—2.1 No
Android 2.2+ Yes
Blackberry 1.0—5.0 No
Blackberry 6.0+ Yes
BB Tablet OS 2.0+ Yes
Firefox OS 1.0 No
iOS 1.0—5.1.1 No
iOS 6.0+ Yes
(function(a,b,c){if(c in b&&b[c]){var d,e=a.location,f=/^(a|html)$/i;a.addEventListener("click",function(a){d=a.target;while(!f.test(d.nodeName))d=d.parentNode;"href"in d&&(chref=d.href).replace(e.href,"").indexOf("#")&&(!/^[a-z\+\.\-]+:/i.test(chref)||chref.indexOf(e.protocol+"//"+e.host)===0)&&(a.preventDefault(),e.href=d.href)},!1)}})(document,window.navigator,"standalone");
// Function for returning the user to any 'y' position in a Facebook app/tab. Uses jQuery animate, otherwise gracefully falls-back without it.
// Source[1]: http://stackoverflow.com/questions/7193425/how-do-you-animate-fb-canvas-scrollto
// Source[2]: https://developers.facebook.com/docs/reference/javascript/FB.Canvas.scrollTo/
var scrollY = function (y) {
if (window.jQuery) {
FB.Canvas.getPageInfo (function (pageInfo) {
$({ y: pageInfo.scrollTop })
.animate({
y: y
@ahsquared
ahsquared / hide text in html
Created August 29, 2013 18:59
hide text in html without the performance issues of using text-indent: -9999px
.hide-text {
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
}
@ahsquared
ahsquared / jquery.styleCheckboxes
Last active December 22, 2015 11:39
select checkboxes to style: $(':checkbox').styleCheckboxes();
// select checkboxes to style: $(':checkbox').styleCheckboxes();
$.fn.styleCheckboxes = function() {
$(this).each(function(index) {
if (!$(this).prev("a").hasClass('check-styled')) {
$(this).before("<a href='#check_" + index + "' class='check-styled' id='check_" + index + "'></a>").css({
position: 'absolute',
left: -1,
top: 3,
zIndex: -1,
visibility: "hidden"