开发者

Javascript onclick get url value

I will like to get a url value upon onclick. like this:

www.google.com/myfile.php?id=123

I want to 开发者_开发技巧get id and its value.


window.location.search will get you the ?id=123 part.


After reading the comments, it looks like you want a way to get the query string off a url, but not the current url.

function getParameters(url){

    var query = url.substr(url.lastIndexOf('?'));

    // If there was no parameters return an empty object
    if(query.length <= 1)
        return {};

    // Strip the ?
    query = query.substr(1);

    // Split into indivitual parameters
    var parts = query.split('&');
    var parameters = {};
    for(var i = 0; i < parts.length; i++) {

        // Split key and value
        var keyValue = parts[i].split('=');
        parameters[keyValue[0]] = keyValue[1] || '';
    }

    return parameters;
}

function alertId(a){
    var parameters = getParameters(a.href);
    alert(parameters.id);
}

//onclick="alertId(this); return false;"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜