使用jquery获取url及url参数的方法

2019-08-28  本文已影响0人  web小哥MrYang

function getUrlParam(name) {

                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");

                    var r = window.location.search.substr(1).match(reg);

                    if (r != null) return unescape(r[2]); return null;

    }

 getUrlParam('id')

高级稳定版*中文地址栏传参数

function getUrlParam(key) {

    // 获取参数

    var url = window.location.search;

    // 正则筛选地址栏

    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");

    // 匹配目标参数

    var result = url.substr(1).match(reg);

    //返回参数值

    return result ? decodeURIComponent(result[2]) : null;

}

例如:http://localhost/help/collection/template/question.html?class=1&id=1-2-3-4

打印结果:1-2-3-4

上一篇 下一篇

猜你喜欢

热点阅读