zoukankan      html  css  js  c++  java
  • window.location获取URL信息

    window的location对象

    • window.location.href 整个URl字符串(在浏览器中就是完整的地址栏)

    • window.location.protocol URL 的协议部分。返回值:http:

    • window.location.host URL 的主机部分(带端口号)

    • window.location.port URL 的端口部分。

    • window.location.pathname URL 的路径部分(就是文件地址)

    • window.location.search 查询(参数)部分。得到的是url中?部分。
      除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值。

    • window.location.hash 锚点。得到的是url中#部分。

    substr()

    返回一个从指定位置开始的指定长度的子字符。
    获取URL中的查询字符串:(这里substr设置为1,是为了把url中的?号去掉)

    function getQuery(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return decodeURI(r[2]); return null;
    }
    

    注:

    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    (^| )代表开始
    ( |$)代表结束
    以&或者$结尾的字符串
    这个正则是寻找&+url参数名字=值+&
    &可以不存在。

  • 相关阅读:
    valgrind检查:Conditional jump or move depends on uninitialised value(s)
    信号 SIGPIPE
    Snapdragon——1.定位游戏瓶颈
    unity修改所选路径下的,对象的importer属性
    git命令行
    ue4 lightmass研究
    leecode保存 简单题到ZY转换
    ue4 skybox
    ue4导入staticMesh
    uml类图的几种关系
  • 原文地址:https://www.cnblogs.com/ZerlinM/p/13570864.html
Copyright © 2011-2022 走看看