HTML
Theme-based stylesheets
<link rel="stylesheet" href="light.css" media="(prefers-color-scheme: light)">
<link rel="stylesheet" href="dark.css" media="(prefers-color-scheme: dark)">
This is not supported by IE and other lesser known browsers.
JavaScript
Asynchronous functions in loops
const start = 0;
const end = 5;
for (i = start; i < end; i++) {
setTimeout(() => {
console.log(i); // -> 5, 5, 5, 5, 5
});
};
function forSync(i) {
setTimeout(() => {
console.log(i); // -> 0, 1, 2, 3, 4
if (i + 1 < end) forSync(i + 1);
});
};
forSync(start);
This allows for asynchronous functions by placing the callback within that function.
Export promise with arguments
const module = require("./module");
module("Hello!").then(console.log); // -> Hello!
module.exports = (input) => {
return new Promise((resolve, reject) => resolve(input));
};
exports
won't work, only module.exports
will!
Get DOM element from jQuery element
$("#foo")[0];
$("#foo").get(0);
Use either, see the jQuery FAQ.
insertAdjacentHTML() positions
<!-- beforebegin -->
<parent>
<!-- afterbegin -->
<child></child>
<!-- beforeend -->
</parent>
<!-- afterend -->
Copied from the MDN documentation.
Swap out variable values
var a = 1;
var b = 2;
[a, b] = [b, a];
console.log(a); // -> 2
console.log(b); // -> 1
You can also specify more (or less) than two variables.
Write to clipboard
navigator.clipboard.writeText("Text").then(function() {
// Success!
}, function() {
// Error!
});
This is not supported in IE, and Firefox hides it behind a flag.
PHP
Temporarily disable Composer cache
COMPOSER_CACHE_DIR=/dev/null composer
See the Composer documentation for details.
Use PHP variables in variable names
$foo = "bar";
$$foo = "baz";
echo $bar; // -> baz
Here, $$foo
resolves to $bar
!
Other
Connect via ZOC from WinSCP
WinSCP → Options → Preferences → Applications → PuTTY/Terminal client path:
"zoc.exe" /CONNECT:!U:!P@!@:!# "/RUN:%USERPROFILE%\Documents\ZOC8 Files\REXX\cd.zrx" "/RUNARG:!/"
%USERPROFILE%\Documents\ZOC8 Files\REXX\cd.zrx:
CALL ZocSend "cd '"ARG(1)"'^M"
Create new PostgreSQL user and database
CREATE DATABASE database;
CREATE USER user WITH ENCRYPTED PASSWORD password;
GRANT ALL PRIVILEGES ON DATABASE database TO user;
Fix error 0x80073D05 when installing Ubuntu on Windows
Remove-Item -Recurse $env:LOCALAPPDATA/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
Re-use blocks in Caddyfile
(foo) {
log
}
example.com {
import foo
}
See the Caddy documentation for details.