zoukankan      html  css  js  c++  java
  • JavaScript获取当前根目录

    主要用到Location 对象,包含有关当前 URL 的信息,是 Window 对象的一个部分,可通过 window.location 属性来访问。

    方法一 (window.document.location.href/window.document.location.pathname)

    function getRootPath_web() {
                //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
                var curWwwPath = window.document.location.href;
                //获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
                var pathName = window.document.location.pathname;
                var pos = curWwwPath.indexOf(pathName);
                //获取主机地址,如: http://localhost:8083
                var localhostPaht = curWwwPath.substring(0, pos);
                //获取带"/"的项目名,如:/uimcardprj
                var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
                return (localhostPaht + projectName);
            }

    方法二(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;
                }
            }

    注:

     1、document默示的是一个文档对象,window默示的是一个窗口对象,一个窗口下可以有多个文档对象。
      所以一个窗口下只有一个window.location.href,然则可能有多个document.URL、document.location.href

    2、window.location.href和document.location.href可以被赋值,然后跳转到其它页面,document.URL只能读不可写

     3、Location 对象详细信息参考w3school http://www.w3school.com.cn/jsref/dom_obj_location.asp

  • 相关阅读:
    多态
    多继承
    传宗接代——继承
    解决vue项目更新版本后浏览器的缓存问题
    escape()、encodeURI()、encodeURIComponent()三种编码方式的区别
    epoll使用总结
    探讨c/c++的指针
    基于linux的pthread_t封装一个Thread类
    unix高并发编程中遇到的问题和笔记
    面向对象分析与设计 实验七
  • 原文地址:https://www.cnblogs.com/dongming-03/p/5698099.html
Copyright © 2011-2022 走看看