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 (ie 404 page address), not what they have typed
This gives you the exact url the user is on:
document.location.href
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.
console.log(' href => ' + window.location.href); console.log(' host => ' + window.location.host); console.log(' hostname => ' + window.location.hostname); console.log(' port => ' + window.location.port); console.log(' protocol => ' + window.location.protocol); console.log(' pathname => ' + window.location.pathname); console.log(' hashpathname => ' + window.location.hash); console.log(' search=> ' + window.location.search);
reference: https://tecadmin.net/get-current-url-web-browser-using-javascript/
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.
当您登陆404页面时,许多内容管理系统会保留URL,因此您应该能够使用document.location.href
,然后只需检查错误页面上的分析。
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.
var arr = [], l = document.links;
for(var i=0; i<l.length; i++) {
arr.push(l[i].href);
}
javascript: alert(window.location.hostname);
如果要显示路径名,请将.hostname
替换为.pathname
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.