Javascript get current url, domain from url, relative path, parameter and parameter value

Lionsure 2020-05-13 Original by the website

The current domain name, Url, relative path, parameter and parameter value can be got separately in Javascript. The so-called separate acquisition means that the domain name does not include the path and parameter of the webpage file, the parameter does not include the domain name and the path of the webpage file.


I. Javascript get domain from url

Method 1:

var domain = document.domain;


Method 2:

var domain =;


3. Pay attention to the problem

Because the current domain name that has been got does not include http://, when assigning it to the href of a tag, don't forget to add http://, otherwise the navigation will be wrong when you click the link.



II. Javascript get current url (four methods)

var url = window.location.href;

var url = self.location.href;

var url = document.URL;

var url = document.location;

What is displayed in the address bar of the browser, what is the got url.



III. How to get the relative path in javascript

First get the Url, then cut the Url into two parts by //, and then cut the relative path from the latter part. If there are parameters in the relative path intercepted, the parameters are removed.

function GetUrlRelativePath()
              var url = document.location.toString();
              var arrUrl = url.split("//");

       var start = arrUrl[1].indexOf("/");
              var relUrl = arrUrl[1].substring(start);// "stop" is omitted, intercept all characters from start to end

       if(relUrl.indexOf("?") != -1){
                     relUrl = relUrl.split("?")[0];
              return relUrl;

Call: GetUrlRelativePath();

Example: If the current URL is, the relative path intercepted is: /pub/item.aspx.


IV. Javascript get url parameter

1. How to get url parameters in javascript

function GetUrlPara()
              var url = document.location.toString();
              var arrUrl = url.split("?");

       var para = arrUrl[1];
              return para;

Call: GetUrlPara()

Example: If the current URL is, the intercepted parameter part is: t=pd&tn=Program+development.


2. Javascript get specific url parameter (How to get url parameter value in javascript)

(1) Implementation process

First get the URL of the currently visited webpage through "document.location", and secondly use "split" method to divide the URL into two parts by "?". If there are parameters in the URL (arrObj.length > 1); then use the "split" method to separate each parameter by "&"; then use the "for" loop to check if there is the same parameter as the one you are looking for, and if so, return the parameters Value; if not, continue to loop until all parameters are found. If there are no parameters in the URL and no parameters are found, both return empty.


(2) The code is as follows:

// paramName is the name of the search parameter
       function GetUrlParam(paramName) {
              var url = document.location.toString();
              var arrObj = url.split("?");

       if (arrObj.length > 1) {
                     var arrParam = arrObj[1].split("&");
                     var arr;

              for (var i = 0; i < arrParam.length; i++) {
                            arr = arrParam[i].split("=");

                     if (arr != null && arr[0] == paramName) {
                                   return arr[1];
                     return "";
              else {
                     return "";

Call: GetUrlParam("id");

For example:

If the URL of the webpage has such a parameter test.htm?id=896&s=q&p=5, then call GetUrlParam(("p"), return 5.