Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active May 16, 2025 11:34
Show Gist options
  • Save vielhuber/dbbe66847013bc25f1a7cb523a0d97a1 to your computer and use it in GitHub Desktop.
Save vielhuber/dbbe66847013bc25f1a7cb523a0d97a1 to your computer and use it in GitHub Desktop.
ctrl strg ajax links pushState #js
/* this ensures that ctrl+links open in a new window */
var ctrlPressed = false;
window.addEventListener('keydown', e => {
if (e.metaKey || e.ctrlKey || e.key === 'Control' || e.key === 'Meta') {
ctrlPressed = true;
}
});
window.addEventListener('keyup', e => {
if (e.metaKey || e.ctrlKey || e.key === 'Control' || e.key === 'Meta') {
ctrlPressed = false;
}
});
document.addEventListener('click', (e) => {
if( ctrlPressed ) {
return true;
}
/* do ajax / pushState etc. */
/* ... */
e.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment