• javascript获取地址栏的绝对路径


    javascript获取地址栏的绝对路径的方法代码:

    1. // 获取地址栏的绝对路径
    2. function getRootPath() {
    3.     var strFullPath = window.document.location.href;
    4.     var strPath = window.document.location.pathname;
    5.     var pos = strFullPath.indexOf(strPath);
    6.     var prePath = strFullPath.substring(0, pos);
    7.     var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    8.     return (prePath + postPath);
    9.     //return prePath;
    10. }

    【getRootPath】获取地址栏的绝对路径的方法名称;

    【window.document.location.href】:获取当前页面地址栏的地址;

    如下图:

    图1

     

    【window.document.location.pathname】:获取当前页面的路径或文件名;

    如下图:

    图2

     

    【strFullPath.indexOf(strPath)】:查看当前页面的路径或文件名在在地址栏之中的位置;

    如下图:

    图3

     

    【 strFullPath.substring(0, pos)】:通过当前页面的路径或文件名的位置截取页面地址栏的地址;

    如下图:

    图4

     

    【strPath.substring(0, strPath.substr(1).indexOf('/') + 1)】:再次做截取;


    【prePath + postPath】:最终获取地址栏的绝对路径;

    如下图:

    图5

     

    这里的截图是用的本地文件打开的,而如果启动服务之后来看的话,会更加清晰。

    关注公众号(月影WEB),了解更多的前后端知识;

    欢迎大家关注互相交流学习;


     

  • 相关阅读:
    【C语言】函数的系统化精讲(三)
    【OpenStack云平台】网络控制节点 HA 集群配置
    VueRouter
    Flink Hudi DataStream API代码示例
    聊聊运行时监控方案
    Redis 之 SessionCallback & RedisCallback 使用
    解决tokenizers缺失问题的Python解决方案
    什么是设计模式?总结
    C++中指针与引用的区别
    react组件父子传值
  • 原文地址:https://blog.csdn.net/qq_34297287/article/details/126824831