How to get the value from URL parameter?

How to get the value from URL parameter?

This is a very common tip in web development. Code is as follow:

function getValue(key) {
    var query = window.location.search.substr(1).split("&");
    for (var i = 0; i < query.length; i++) {
        var param = query[i].split("=");
        if (param.length != 2) {
            continue;
        }
        if (param[0] == key) {
            return param[1];
        }
    }
}

Yang Zhao

Read more posts by this author.


Comment