Skip to content

Instantly share code, notes, and snippets.

@leereamsnyder
leereamsnyder / wantsJSON.js
Last active Oct 20, 2017
Detect if a request in Express is for HTML or JSON
View wantsJSON.js
/*
Usage:
var jsoncheck = require('./wantsJSON') // path to file with this middleware function
app.use(jsoncheck)
app.get('/', function(req,res,next){
if (req.wantsJSON) {
// serve JSON
}
if (req.wantsHTML) {
@leereamsnyder
leereamsnyder / jquery.restoreIOSscrollTo.js
Last active May 27, 2016
Restore window.scrollTo in iOS Safari after tapping the status bar
View jquery.restoreIOSscrollTo.js
/**
**UPDATE**: This was fixed in iOS 8. But this could still be valid for iOS 7 and below.
I believe this was first noticed here: http://blog.b123400.net/window-scrollto-and-ios-status-bar/
> When the status bar is tapped, the page scrolls to the top and url bar is shown,
> then window.scrollTo doesn't work anymore.
Yep!
@leereamsnyder
leereamsnyder / wordpress_listing_excerpt_or_more.php
Last active Aug 29, 2015
In WordPress loops, show the excerpt, the content before <!--MORE-->, or something else, but avoid showing the full post content.
View wordpress_listing_excerpt_or_more.php
<?php
/**
* Let's say you want to support BOTH post excerpts and use of the <!--more--> tag
* to truncate post content in The Loop.
*
* And let's also say that you definitely do not want to show the full content of the post
* if either of those are absent. Maybe you're displaying posts in little Pinterest-y
* cards so the text/excerpt can't be too long.
*
* Let's say this is your priority:
@leereamsnyder
leereamsnyder / wp_get_current_page_url.php
Last active Oct 11, 2019
In WordPress, get current URL including query string
View wp_get_current_page_url.php
<?php
/**
Re-use some existing WordPress functions so you don't have to write a bunch of raw PHP to check for SSL, port numbers, etc
Place in your functions.php (or re-use in a plugin)
If you absolutely don't need or want any query string, use home_url(add_query_arg(array(),$wp->request));
Hat tip to:
+ http://kovshenin.com/2012/current-url-in-wordpress/
@leereamsnyder
leereamsnyder / jquery.attemptfocus.js
Last active Sep 10, 2015
A little jQuery plugin that I've found helps a bunch when managing focus. Adds ':focusable' and ':tabbable' selectors (from jQuery UI) and adds chainable methods to set and manage focus amongst sets of elements
View jquery.attemptfocus.js
;(function($, window, document, undefined){
/* :focusable and :tabbable selectors from
https://raw.github.com/jquery/jquery-ui/master/ui/jquery.ui.core.js */
function visible(element) {
return $.expr.filters.visible(element) && !$(element).parents().addBack().filter(function () {
return $.css(this, "visibility") === "hidden";
}).length;
}
@leereamsnyder
leereamsnyder / jquery.ontransitionend.js
Last active Mar 22, 2017
Quick jQuery plugin to fire the correct browser-prefixed 'transitionend' event with a callback
View jquery.ontransitionend.js
/***********************************
Little utility function for handling single transitionEnd events.
Particularly handy for when you need to remove something from
the DOM after transforming it with an animation
WHY NOT $el.one('transitionend', callback) ????
=============================================