Skip to content

Instantly share code, notes, and snippets.

🤑
Looking for work

Barney Carroll barneycarroll

View GitHub Profile
@barneycarroll
barneycarroll / jquery.notextSelector.js
Last active Oct 12, 2018
`:notext` is an alternative to the `:empty` selector for jQuery. Elements consisting exclusively of an amount of whitespace or HTML comments will be returned.
View jquery.notextSelector.js
/*
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
Hide the page body until page & dependencies have finished loading
View html.loading.snippet.html
<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 Jun 15, 2011
Return an object model of a URI query (search or hash)
View uriQueryObj.js
/* 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 Jul 21, 2011
Simple function to determine whether a given CSS property is supported or not. Useful for conditional execution of JS animations, rounded corners, etc.
View detectCSS.js
// 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 Jul 26, 2011
Fix bad font anti-aliasing
View fixText.css
.background {
background: rgb(247,247,247);
}
.fixText {
text-shadow: 0 0 1px rgba(247,247,247,.5);
}
@barneycarroll
barneycarroll / onload.js
Created Sep 19, 2011
Stack window.onload functions
View onload.js
// 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 Sep 20, 2011
Stop iframes delaying page load
View asyncFrames.js
// 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 Sep 28, 2011
Cross-browser method for injecting passed CSS into the document
View writeStyle.js
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 Sep 29, 2011
Get all an element's attributes & values as a matrix
View attrs.js
// 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,
View $.outerHTML.js
// 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();
};
You can’t perform that action at this time.