Home > Web Front-end > JS Tutorial > javaScript query string parameters get

javaScript query string parameters get

高洛峰
Release: 2016-10-13 11:21:22
Original
1078 people have browsed it

function getQueryStringArgs() {
    //取得查询字符串并去掉开头的问号
    var qs = (location.search.length > 0 ? location.search.substring(1) : "");

    //保存数据对象
    args = {};

    //取得每一项
    items = qs.length ? qs.split("&") : [],
    item = null;
    name = null;
    value = null;

    //for循环
    i = 0;
    len = items.length;

    //将每一项加入args对象中
    for (i = 0; i < len; i++) {
        item = items[i].split("=");
        name = decodeURIComponent(item[0]);
        value = decodeURIComponent(item[1]);

        if (name.length) {
            args[name] = value;
        }
    }
    return args;
}

//假设 ?q=javascript&num=10
var args = getQueryStringArgs();
alert(args["q"]);
alert(args["num"]);
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template