zoukankan      html  css  js  c++  java
  • 关于地址跳转的参数

    //http://www.huistd.com/?id=99&ttt=3&haha=33 
    // GetQueryString("haha")
    //获取地址栏参数
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    
    //changeUrlArg(url, 'haha', 33); // http://www.huistd.com/?id=99&ttt=3&haha=33 
    //修改地址栏参数
    function changeUrlArg(url, arg, val) {
        var pattern = arg + '=([^&]*)';
        var replaceText = arg + '=' + val;
        return url.match(pattern) ? url.replace(eval('/(' + arg + '=)([^&]*)/gi'), replaceText) : (url.match('[?]') ? url + '&' + replaceText : url + '?' + replaceText);
    }
    

      

    在WEB开发中,时常会用到javascript来获取当前页面的url网址信息,在这里是我的一些获取url信息的小总结。

    下面我们举例一个URL,然后获得它的各个组成部分:http://i.cnblogs.com/EditPosts.aspx?opt=1

    1、window.location.href(设置或获取整个 URL 为字符串)

    var test = window.location.href;
    alert(test);
    返回:http://i.cnblogs.com/EditPosts.aspx?opt=1

    2、window.location.protocol(设置或获取 URL 的协议部分)

    var test = window.location.protocol;
    alert(test);
    返回:http:

    3、window.location.host(设置或获取 URL 的主机部分)

    var test = window.location.host;
    alert(test);
    返回:i.cnblogs.com

    4、window.location.port(设置或获取与 URL 关联的端口号码)

    var test = window.location.port;
    alert(test);
    返回:空字符(如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符)

    5、window.location.pathname(设置或获取与 URL 的路径部分(就是文件地址))
    var test = window.location.pathname;
    alert(test);
    返回:/EditPosts.aspx

    6、window.location.search(设置或获取 href 属性中跟在问号后面的部分)

    var test = window.location.search;
    alert(test);
    返回:?opt=1

    PS:获得查询(参数)部分,除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值。

    7、window.location.hash(设置或获取 href 属性中在井号“#”后面的分段)

    var test = window.location.hash;
    alert(test);
    返回:空字符(因为url中没有)

    参考链接:http://www.jb51.net/article/82519.htm

  • 相关阅读:
    Lucene
    coreseek
    Sphinx学习之sphinx的安装篇
    在Hive中使用Avro
    Hive中实现group concat功能(不用udf)
    Fresnel Reflection
    安装Windows更新程序遇到错误:0x80070422
    float4与half4数据类型
    Dijkstra算法(三)之 Java详解
    地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了
  • 原文地址:https://www.cnblogs.com/qqing/p/8436683.html
Copyright © 2011-2022 走看看