I wanted to do some js analytics, so I would need to know how to get whatever the user entered in the address bar as a js variable so I can know what are the most common misspellings. That way I can make redirects for the most common misspellings to the correct adresses and reduce 404 page requests.
example of user input in browser:
https://stackoverflow.com/questions
.........................................
I have tried using
document.location
but that shows what page the user is on (i.e. 404 page address), not what they have typed
If you want to show the pathname, replace
.hostname
with.pathname
.This is a good way to get the Reload link address, if there is one, which should have the URL that was typed in to the address bar.
from: https://stackoverflow.com/a/3871370/1188090
You're going to have to do this on the server, since that's where the original 404 response comes from. The server definitely receives the bad URL, so all that has to happen is that you make your server preserve those somewhere.
This gives you the exact url the user is on:
There is no way of determining what the user typed before the request is submitted (for security-reasons).
JavaScript provides you many methods to get the current URL displaying in web browser address bar. You can use Location object property of the Window object to get these details.
reference: https://tecadmin.net/get-current-url-web-browser-using-javascript/
Many content management systems preserve the url when you land on the 404 page, so you should be able to use
document.location.href
, then just check the analytics on the error page.