Skip to content

Instantly share code, notes, and snippets.

View barneycarroll's full-sized avatar
🛠️
Working on Mithril type stuff

Barney Carroll barneycarroll

🛠️
Working on Mithril type stuff
View GitHub Profile
@barneycarroll
barneycarroll / jquery.notextSelector.js
Last active January 14, 2024 07:57
`:notext` is an alternative to the `:empty` selector for jQuery. Elements consisting exclusively of an amount of whitespace or HTML comments will be returned.
/*
jQuery's ':empty' selector only returns true if the element contains no text node, or whose text node consists exclusively of zero or more spaces and tabs. This ':notext' selector will return true for elements whose text nodes can also contain line breaks (any whitespace character) and HTML comments.
*/
$.expr[':'].notext = function detectNoText(x){
return x.innerHTML && x.innerHTML.replace(/(<!--.*(?!-->))|\s+/g, '').length === 0
}
@barneycarroll
barneycarroll / html.loading.snippet.html
Created May 19, 2011 16:55
Hide the page body until page & dependencies have finished loading
<script>
// add a class of 'loading' to the HTML, then remove it once the page has finished loading
(function(c){
c('scripted loading')
window.onload = function(){setTimeout(function(){
c(c().replace('loading',''))
},30)}
}(function(c){
var h = document.lastChild
return c ? h.className = c : h.className
@barneycarroll
barneycarroll / uriQueryObj.js
Created June 15, 2011 09:36
Return an object model of a URI query (search or hash)
/* Return an object model of a URI query (search or hash) */
function uriQueryObj(str){
if(!str) return false;
var str = decodeURIComponent(str),
str = /(^#)|(^$)/.test(str) ? str.substr(1) : str,
arr = str.substr(1).split('&'),
hash = {};
while(arr.length && arr[0]){
var bits = arr.pop().split('=')
@barneycarroll
barneycarroll / detectCSS.js
Created July 21, 2011 08:23
Simple function to determine whether a given CSS property is supported or not. Useful for conditional execution of JS animations, rounded corners, etc.
// Is the passed CSS property supported?
// eg. detectCSS('transition')
function detectCSS(prop){
var
prop = prop.replace(/-(\w)/g,function(s,g){return g.toUpperCase()}),
pre = ',Icab,Khtml,Moz,Ms,O,Webkit'.split(',');
for (var i = 0; i < pre.length; ++i){
if(i==1)
prop = prop.slice(0,1).toUpperCase() + prop.slice(1);
@barneycarroll
barneycarroll / fixText.css
Created July 26, 2011 10:24
Fix bad font anti-aliasing
.background {
background: rgb(247,247,247);
}
.fixText {
text-shadow: 0 0 1px rgba(247,247,247,.5);
}
@barneycarroll
barneycarroll / onload.js
Created September 19, 2011 11:04
Stack window.onload functions
// Stack window.onload functions
function addLoad(func){
var old = window.onload;
func instanceof Function
&& (window.onload =
old instanceof Function
? function(){func();old()}
: func);
};
@barneycarroll
barneycarroll / asyncFrames.js
Created September 20, 2011 09:47
Stop iframes delaying page load
// iframes delay page load: cut them out and in to reduce synchronous dependency
function asyncFrames(){
$('iframe').each(function(){
var
markup = $(this).clone(),
guide = $('<b>').replaceAll(this);
// addLoad: https://gist.github.com/1226302
addLoad(function(){
guide.replaceWith(markup)
@barneycarroll
barneycarroll / writeStyle.js
Created September 28, 2011 16:52
Cross-browser method for injecting passed CSS into the document
function writeStyle(rules){
var head = document.getElementsByTagName('head')[0],
el = this.el || document.createElement('style'),
rules = rules || '';
if(!this.el)
el.type = 'text/css';
if(el.styleSheet){
el.styleSheet.cssText = rules;
@barneycarroll
barneycarroll / attrs.js
Created September 29, 2011 11:36
Get all an element's attributes & values as a matrix
// Return all attributes, optionally filtered by RegExp
$.fn.attrs = function(expr){
var
el = this[0],
$el = this,
attrArray = [];
for(var i=0, attrs = el.attributes; i<attrs.length; ++i){
var attrKey = [
attrs.item(i).nodeName,
@barneycarroll
barneycarroll / $.outerHTML.js
Created September 29, 2011 11:37
$.outerHTML()
// Cross-browser outerHTML: return it, or if an argument was passed, replace the element.
$.fn.outerHTML = function(arg){
var $el = this;
return arg
? $el.replaceWith(arg)
: $el.clone().wrap('<b>').parent().html();
};