I’d like to display different content based on window.navigator.userAgent
, but it doesn’t seem to be updated when you change it via a browser’s devtools.
I want to automatically refresh the page when the user agent is changed using the devtools, to prevent use of a page meant for the actual device/browser instead of the one specified with the devtools.
2
Answers
Use
setInterval()
to check periodically to see if it has changed.The
userAgent
can only change if the person viewing the site opens up their DevTools and manually changes it. Since this is not really a normal use case, there is no event to watch for when theuserAgent
changes. The only way to do this—that I’m aware of—is to manually poll for changes (e.g., withsetInterval
):