Javascript get referrer url(previous page url), current domain and url

Lionsure 2020-06-06 Original by the website

In the process of web page design, regardless of the foreground or background, we will get the url of the previous or current page, we get it in the background using a programming language (such as C#, PHP), and get it in the foreground only use javascript. Javascript can not get the url of the previous page every time, only when there is a previous page, that is, it can be got when you click on a link on the website to open the current page, if you directly enter the url in the address bar of browse, it cannot got.

Javascript uses the document.referrer to get the url of the previous page, and the document.location to get the url of the current page. In addition, you can directly get the domain name of the current page by using document.domain.

 

1. Get previous page url javascript(i.e. Javascript get referrer url) with document.referrer

Javascript code:

<script type="text/javascript">
       function GetReferrer() {
              var preUrl = document.referrer;
              if (preUrl == null)
                     return "The previous page url is empty";
               else
                     return preUrl;
       }
       </script>

The above code to get the url of the previous page is directly encapsulated into a method. If the url of previous page is got, it is returned; if it is not got, the prompt text is returned, of course, you can also return empty.

 

2. Javascript get current domain with document.domain

Javascript code:

function GetDomain() {
              var domain = document.domain;
              if (domain == null)
                     return "Failed to get domain name";
              else
                     return domain;
       }

This method will hardly fail to get the domain name of current page unless the code has an error or an exception occurs. Only get the domain name, not the path of the web file. If debugging locally, only get localhost.

 

3. Javascript get referrer domain

Javascript code:

function GetReferrerDomain() {
              var preUrl = document.referrer;
              if (preUrl == null) {
                     return "Failed to get the domain name of the previous page";
              }
              else {
                            return preUrl.split('/')[2];//Split the previous page url into an array with a slash, the third element is the domain name
                     }
              }

Or

function GetReferrerDomain() {
              var preUrl = document.referrer;
              if (preUrl == null) {
                      return "Failed to get the domain name of the previous page";
              }
              else {
                     var a = document.createElement('a');//Create a hyperlink element "a"
                     a.href = preUrl;
                     return a.hostname;//Returns the host name of a, the domain name
              }
       }

 

4. Javascript get current url with document.location.href

Javascript code:

function GetCurrentUrl() {
              var url = document.location.href;
              if (url == null)
                     return "";
              else
                     return url;
       }

If the current page url is got, it is returned; if it is not got, empty is returned, of course, this may hardly occur.