JavaScript gets current URL root directory (path) _javascript tips

Source: Internet
Author: User

The main use of the Location object, containing information about the current URL, is a part of the Window object that can be accessed through the Window.location property.

Method One (Window.document.location.href/window.document.location.pathname)------------transferred from Network

function Getrootpath_web () {
  //get current URL, such as: Http://localhost:8083/uimcardprj/share/meun.jsp
  var curwwwpath = Window.document.location.href;
  Gets the directory after the host address, such as: Uimcardprj/share/meun.jsp
  var pathName = window.document.location.pathname;
  var pos = curwwwpath.indexof (pathName);
  Get host address, such as: http://localhost:8083
  var localhostpaht = curwwwpath.substring (0, POS);
  Get the project name with "/", such as:/uimcardprj
  var projectname = pathname.substring (0, Pathname.substr (1). IndexOf ('/') + 1);
  Return (localhostpaht + projectname);
}

Method Two (Window.location.pathname/window.location.protocol/window.location.host)

function getrootpath_dc () {
  var pathName = window.location.pathname.substring (1);
  var WebName = PathName = = "? ': pathname.substring (0, Pathname.indexof ('/'));
  if (WebName = = "") {return
    Window.location.protocol + '//' + window.location.host;
  }
  else {return
    Window.location.protocol + '//' + Window.location.host + '/' + webName;
  }
}}

Note:

1, the document implied is a Documentation object, window is implied by a Windows object, a window can have more than one document object.
So there is only one window.location.href under a window, but there may be multiple document.url, Document.location.href------------from the network

2, Window.location.href and document.location.href can be assigned value, and then jump to other pages, Document.url can only read or write------------transferred from the network

3, Location object detail information Reference W3school http://www.jb51.net/w3school/jsref/dom_obj_location.htm

Small series of cloud-dwelling communities supplement:

Exclude certain directories

var pathName = window.document.location.pathname;
var projectname = pathname.substring (1, PATHNAME.SUBSTR (1). IndexOf ('/') + 1);
var ad_projectlist = ',, Web,html5,css, ';
if (Ad_projectlist.indexof (', ' +projectname+ ', ') < 0) {
 alert ("web,html5,css several directory codes do not execute");
}

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.