How to get URL from browser address bar?

2020-07-02 10:56发布

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

6条回答
啃猪蹄的小仙女
2楼-- · 2020-07-02 11:12
javascript: alert(window.location.hostname);

If you want to show the pathname, replace .hostname with .pathname.

查看更多
放荡不羁爱自由
3楼-- · 2020-07-02 11:20

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);
}

from: https://stackoverflow.com/a/3871370/1188090

查看更多
smile是对你的礼貌
4楼-- · 2020-07-02 11:25

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.

查看更多
一纸荒年 Trace。
5楼-- · 2020-07-02 11:26

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).

查看更多
Root(大扎)
6楼-- · 2020-07-02 11:27

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.

  1. href => This will return the entire URL displaying in the address bar.
  2. host => This will return the hostname and port of the URL in address bar.
  3. hostname => This will return only the hostname of the URL in address bar.
  4. port => This will return only the port detail of the URL in address bar.
  5. protocol => This will return the protocol of the URL in address bar. Like the URL is using HTTP (without SSL) or HTTPS (with SSL).
  6. pathname => This will return the pathname (value after the domain name) of the URL in address bar.
  7. hashpathname => This will return the anchor portion of the URL including hash singh (#).
  8. search=> This will return the query portion of the URL like portion started with the question mark (?).
    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/

查看更多
Fickle 薄情
7楼-- · 2020-07-02 11:27

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.

查看更多
登录 后发表回答