码迷,mamicode.com
首页 > 编程语言 > 详细

用JavaScript获取地址栏参数的方法

时间:2014-12-04 13:42:44      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   color   os   使用   sp   

/**
 *  获取地址栏参数
 *
 * @example GetUrlString(‘id‘)
 *
 * @desc    调用时加上判断,保证程序不会出错
 *             var myurl = GetUrlString(‘id‘);
 *             if (myurl != null && myurl.toString().length > 1) {
 *                  alert(GetUrlString("id"));  
 *             }
 *
 * @param String  param 要获取地址栏中的参数名
 * @return String Value
 * @type String
 *
 * @name GetUrlString()
 *
 * @author Iceman / 254784109@qq.com
 */

function GetUrlString(param) {
    var sValue = location.search.match(new RegExp("[\?\&]" + m + "=([^\&]*)(\&?)", "i"));
    return sValue ? decodeURI(sValue[1]) : decodeURI(sValue);
}

调用的时候统一做这样的判断,避免你没有传参数的话,比如你的地址是 abc.html 后面没有参数,那强行输出调用结果有的时候会报错

window.onload = function() {    
    var myurl = GetParm("id");
    if (myurl != null && myurl.toString().length > 1) {
        alert(GetParm("id"));
    }
}

这样就不会报错了!

注释:ECMAScript v3 已从标准中删除了 unescape() 函数,并反对使用它,因此应该用 decodeURI() 和 decodeURIComponent() 取而代之。

用JavaScript获取地址栏参数的方法

标签:des   style   blog   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/IcemanZB/p/4142484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!