Last active
May 16, 2025 11:34
-
-
Save vielhuber/dbbe66847013bc25f1a7cb523a0d97a1 to your computer and use it in GitHub Desktop.
ctrl strg ajax links pushState #js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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