提取url字段

获取浏览器地址:var url = window.location.href;

判断地址中是否存在某字段:

例如:http://localhost:36666/ChooseClientView?key=A8D2DB67-952B-4BB2-A0F1-4BB083CFB55B&search=上海

window.location结构:

var url = window.location.href;
if (url.indexOf("search") != -1) { 
    var params = (function () {
        var search = location.search;
        var params = {};
        if (search != "") {
            search.slice(1).split("&").forEach(
                function (val) {
                    var arr = val.split("=");
                    params[arr[0]] = arr[1];
                }
            );
        }
        return decodeURI(params.search);
    })();
if (url.indexOf("search") != -1) //如果url中存在search
var search = location.search; //?key=A8D2DB67-952B-4BB2-A0F1-4BB083CFB55B&search=%E4%B8%8A%E6%B5%B7
search.slice(1)//截取字符串,结果为key=A8D2DB67-952B-4BB2-A0F1-4BB083CFB55B&search=%E4%B8%8A%E6%B5%B7
search.slice(1).split("&")//用于把一个字符串分割成字符串数组。结果为:["key=A8D2DB67-952B-4BB2-A0F1-4BB083CFB55B", "search=%E4%B8%8A%E6%B5%B7"]
            search.slice(1).split("&").forEach(
                function (val) {
                    var arr = val.split("=");
                    params[arr[0]] = arr[1];
                }
            );
//结果为{key: "A8D2DB67-952B-4BB2-A0F1-4BB083CFB55B", search: "%E4%B8%8A%E6%B5%B7"}

return decodeURI(params.search);//将乱码转为汉字结果为  上海
 
posted @ 2018-01-31 13:55  cacti_vc  阅读(601)  评论(0编辑  收藏  举报