codetc - 网站开发技术 首页 前端 查看内容

Javascript获取URL参数值和当前页面URL信息

2015-5-1 22:34| 发布者: CODETC| 查看: 4888| 评论: 0

在开发中我们通常会遇到需求需要获取当前URL的参数信息和URL等信息,这时候需要配合javascript系统的方法和正则表达式来实现代码,


1.获取URL参数值方法

function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        if (str.indexOf("&") != -1) {
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
            }
        } else {
            theRequest[str.split("=")[0]] = unescape(str.split("=")[1]);
        }
    }
    return theRequest;
}

调用上面的方法:


<script language="javascript">
var Request = new Object();
Request = GetRequest();
var 参数1,参数2,参数3,参数N;
参数1 = Request['参数1'];
参数2 = Request['参数2'];
参数3 = Request['参数3'];
参数N = Request['参数N'];
</script>

2.获取URL详细信息

设置或获取对象指定的文件名或路径

window.location.pathname
设置或获取整个 URL 为字符串

window.location.href
设置或获取与 URL 关联的端口号码

window.location.port
设置或获取 URL 的协议部分

window.location.protocol
设置或获取 href 属性中在井号“#”后面的分段

window.location.hash
设置或获取 location 或 URL 的 hostname 和 port 号码

window.location.host
设置或获取 href 属性中跟在问号后面的部分

window.location.search
所有获取与URL有关的信息都用到了window.location对象,这个对象中包含了url信息,具体的属性列表如下:

hash 设置或获取 href 属性中在井号“#”后面的分段。
host 设置或获取 location 或 URL 的 hostname 和 port 号码。
hostname 设置或获取 location 或 URL 的主机名称部分。
href 设置或获取整个 URL 为字符串。
pathname 设置或获取对象指定的文件名或路径。
port 设置或获取与 URL 关联的端口号码。
protocol 设置或获取 URL 的协议部分。
search 设置或获取 href 属性中跟在问号后面的部分。

文章来源 CODETC,欢迎分享,转载请注明地址: http://www.codetc.com/article-187-1.html

最新评论

 作为游客发表评论,请输入您的昵称

返回顶部