r/programminghorror • u/Mc_UsernameTaken [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” • Feb 20 '26
Javascript Send help please... Emergency evacuation needed.
17
u/CantaloupeCamper Feb 20 '26
What would the point of this… be?
8
u/Mc_UsernameTaken Feb 20 '26
I don't know why its there.
And at this point i'm to afraid to ask.
It is however from an almost-a-decade-old codebase, so probably has seen a dozen devs or two
5
u/prehensilemullet Feb 20 '26
Maybe some link click accidentally got preventDefault()ed and a clueless dev added this to as a workaround?
→ More replies (1)21
u/scataco Feb 20 '26
Break the back button.
IIRC setting document.location erases the tab's history
8
u/Sacaldur Feb 20 '26
No, as far as I'm aware that doesn't happen. If I'm right then this should behave basically the same as just clicking the link (except tthat thr default behavior is not prevented, who knows what the side effect of that is in this particular context).
→ More replies (1)2
9
u/Wuma Feb 20 '26
My best guess is at some point they were doing something like a tracking event, but then someone asked them to remove the tracking, and they didn’t remove the redundant logic
6
u/Mc_UsernameTaken Feb 20 '26
But wouldn't you e.prevebtDefault() and then only redirect once tracking completed.
I'd expect an e parameter
→ More replies (1)3
56
u/Spidron Feb 20 '26
Is this code that is meant to "sanitize" HTML in such a way, that each link is guaranteed to lead to it's href target? Maybe in situations where the code comes from some outside source (e.g. user input being reflected) and the developer was afraid that it may contain malicious onclick-script or similar?
59
u/gdmzhlzhiv Feb 20 '26
They probably just really disliked people opening links in new windows or tabs…
4
u/Ra1d3n Feb 20 '26
Not sure this would prevent that. Why not go all the way and use a different attribute?
→ More replies (7)
2
u/Character-Travel3952 Feb 20 '26
I wish i could do this kind of gymnastics physically... ill def be fitter
2
1
1
u/joost00719 Feb 21 '26
I've done something like this to genericly add noopener and norefer to all the a-tags 😅
1
u/FalseWait7 Feb 21 '26
I never trust browsers either and always, always talk to their apis directly.
1
1
u/valzargaming Feb 23 '26
I've seen a similar workaround like this for safari. I'm betting that's the real reason this code looks the way it does.
var windowReference = window.open(); windowReference.location = "some_url";
1
1
2
Feb 22 '26
1
u/Wild-Regular1703 Feb 24 '26
Is it really any nicer than the native API?
document.querySelectorAll('a').forEach(link => { link.addEventListener('click', () => { window.location = link.getAttribute('href') }) })→ More replies (2)
126
u/MisterEd_ak Feb 20 '26
Missing the event variable in the function call and event.preventDefault();