Skip to content

Instantly share code, notes, and snippets.

@camerond
Created February 14, 2014 16:17
Show Gist options
  • Save camerond/9003958 to your computer and use it in GitHub Desktop.
Save camerond/9003958 to your computer and use it in GitHub Desktop.
$(':absfocus') selector for inactive windows
// Finds focused elements on page even if the browser window isn't active.
// Useful for headless testing or QUnit tests or whatever.
//
// I couldn't get https://github.com/mattheworiordan/jquery-focus-selenium-webkit-fix
// to behave in jQuery 2.0, so this is a secondary selector created using
// Sizzle's (relatively) new createPseudo method.
//
// I'd prefer to rewrite :focus but couldn't get that to work either.
// Look, this works and I'm moving on.
jQuery.expr[":"].absfocus = jQuery.expr.createPseudo(function() {
return function(elem) {
var doc = elem.ownerDocument;
return elem === doc.activeElement && !!(elem.type || elem.href);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment