How to get SITE URL in javascript

May 26, 2022 · 1 min read

Below is the code that help to get the site url in javascript, the purpose of this code is Sometime we need the site url in using js, in mine case i need to call ajax and want to set site base url so I have used this code and passes this in ajax URL.

function getRootUrl() {
  var s = location.pathname.split("/");
  if ("localhost" == location.host) {
    var e = location.origin + "/" + s[1].trim("/") + "/";
  } else if ("localhost:3000" == location.host) {
    var e = location.origin + "/";
  } else {
    var e = location.origin + "/";
  }
  return e;
}
How to block file download outside from Website using .htaccess
How to maintain the scroll of page we have visited previously using localStorage
Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Pin on Pinterest
Pinterest