zoukankan      html  css  js  c++  java
  • js中获取项目路径的小插件

    //立即执行的js
    (function() {
        //获取contextPath
        var contextPath = getContextPath();
        //获取basePath
        var basePath = getBasePath();
        //将获取到contextPath和basePath分别赋值给window对象的g_contextPath属性和g_basePath属性
        window.g_contextPath = contextPath;
        window.g_basePath = basePath;
    })();
    
    /**
     * @author 孤傲苍狼
     * 获得项目根路径,等价于jsp页面中
     *  <%
            String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        %>
     * 使用方法:getBasePath();
     * @returns 项目的根路径
     *  
     */
    function getBasePath() {
        var curWwwPath = window.document.location.href;
        var pathName = window.document.location.pathname;
        var pos = curWwwPath.indexOf(pathName);
        var localhostPath = curWwwPath.substring(0, pos);
        var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
        return (localhostPath + projectName);
    }
    
    /**
     * @author 孤傲苍狼
     * 获取Web应用的contextPath,等价于jsp页面中
     *  <%
            String path = request.getContextPath();
        %>
     * 使用方法:getContextPath();
     * @returns /项目名称(/EasyUIStudy_20141104)
     */
    function getContextPath() {
        return window.document.location.pathname.substring(0, window.document.location.pathname.indexOf('/', 1));
    };

    我从别人那里偷过来的:

    https://home.cnblogs.com/u/xdp-gacl/

  • 相关阅读:
    iOS学习13之OC NSString类
    iOS之08-核心语法
    iOS学习12之OC属性和点语法
    iOS学习11之OC继承
    iOS之07-三大特性之多态 + NSString类
    iOS学习10之OC类和对象
    swift(三)字典数组操作
    swift(二)swift字符串和字符和逻辑运算
    swift(二)swift字符串和字符和逻辑运算
    swift(一)基础变量类型
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/6090370.html
Copyright © 2011-2022 走看看