NULL
NULL

Reputation: 1589

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.

Upvotes: 3

Views: 4959

Answers (2)

Gordon Tucker
Gordon Tucker

Reputation: 6773

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;"

Upvotes: 1

naivists
naivists

Reputation: 33511

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

Upvotes: 3

Related Questions