Skip to content

Instantly share code, notes, and snippets.

View erikvorhes's full-sized avatar

Erik Vorhes erikvorhes

View GitHub Profile
@erikvorhes
erikvorhes / focusHash.js
Last active January 25, 2023 17:43
Ensures an ID matching the `window.location.hash` can and will receive focus if it isn't already focused.
/**
* Focuses an element even if it isn't focusable.
* @param {HTMLElement} target - element to focus
*/
function focusTarget(target) {
if (document.activeElement === target) {
return;
}
if (target.tabIndex < 0 && !target.getAttribute('tabIndex')) {
@erikvorhes
erikvorhes / errorHelpMaybe.js
Created June 4, 2018 18:54
Maybe you need help with an error; maybe you’ll find help with that same error
const errorHelpMaybe = (func) => {
try {
func();
} catch (err) {
const { name, message } = err;
const url = `https://stackoverflow.com/search?q=${encodeURIComponent(`${name} ${message}`)}`;
console.log(name, message, url);
window.open(url, `StackOverflow${Math.random()}`);
throw err;
}
@erikvorhes
erikvorhes / marquee.js
Last active March 15, 2018 18:34
You need a marquee.
/**
* This marquee stuff is deliberately low-tech. Use at your own risk. Licensed under whatever license you need.
* Also available as a part of https://github.com/erikvorhes/Useless-JS
*
* Usage: `marquee();`
*/
function marquee(behavior, direction) {
var behaviors = ['scroll', 'slide', 'alternate'];
var directions = ['left', 'right', 'up', 'down'];
@erikvorhes
erikvorhes / responsive-images.html
Created February 1, 2012 16:29
Responsive Images
<!-- NOTE: This doesn't work reliably. DO NOT USE. Thx. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Responsive Images</title>
<!-- A variation on http://www.monoliitti.com/images/ -->
<!-- Note: the `big` version of the image will be broken, as it doesn’t exist. -->
</head>
<body>
@erikvorhes
erikvorhes / fantest.js
Created August 1, 2011 20:53
Computer feature test of the highest order.
(function fanTest () {
'use strict';
var i = 1;
while (i) {
i = i + 1;
// Let's be nice and not throw errors in browsers that don't have consoles ...
if (!!console) {
i < 10000 && console.log('These fans work great!');
i >= 10000 && console.log('Listen to the music those fans make!');
}