本实例初始化:
if (typeof xcsoft == 'undefined') var xcsoft = {};
xcsoft.urlParam = {};方法一:采用正则表达式获取地址栏参数:(强烈推荐,既实用又方便!)
xcsoft.urlParam.RegExp = function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null
}
//调用方法
xcsoft.urlParam.RegExp('name')方法二:采用循环获得所有参数,需要获得所有参数的可以考虑.
xcsoft.urlParam.normal = function(name) {
    var url = window.location.search;
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1])
        }
    }
    if (name) return theRequest[name];
    else return theRequest
}
//调用方法:获得一个参数
xcsoft.urlParam.normal('name');
xcsoft.urlParam.normal()['name'];
//调用方法:获得所有参数
xcsoft.urlParam.normal()补充知识点:
对于这样一个URL
http://www.xcsoft.cn/article/497.html?url=http://www.xcsoft#top
我们可以用javascript获得其中的各个部分
1, window.location.href
整个URl字符串(在浏览器中就是完整的地址栏)
本例返回值: http://www.xcsoft.cn/article/497.html?url=http://www.xcsoft#top
2,window.location.protocol
URL 的协议部分
本例返回值:http:
3,window.location.host
URL 的主机部分
本例返回值:www.xcsoft.cn
4,window.location.port
URL 的端口部分
如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符
本例返回值:""
5,window.location.pathname
URL 的路径部分(就是文件地址)
本例返回值:article/497.html
6,window.location.search
查询(参数)部分
除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值
本例返回值:?url=http://www.xcsoft
7,window.location.hash
锚点
本例返回值:#top
标签 技巧
按键盘左右方向键可快速浏览上一篇(←)、下一篇(→)
| 您的姓名: | |
| 上一步:鼠标向上滚动/左方向键(←) 下一步或完成:鼠标向下滚动/Tab键/右方向键(→) | 
| 您的电话: | |
| 您的邮箱: | 
| 信息内容: | |
| 验证码: |