views:

497

answers:

2

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.

thanks

+2  A: 

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

naivists
i tried that, there was no value; i just alerted it upon onlicking the function getVal(){event. alert(window.location.search)}www.google.com/myfile.php?id=123 onclick="getVal"
Menew
+1  A: 

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;"
Gordon Tucker
i got undefined upon onclick hmmm
Menew
made the change but still says undefined..hmmm
Menew
no luck still ;-(
Menew
i think maybe of the way its been called. the pages get loaded already on the screen. the url is not displayed on the location bar either.but when you mouse over the thumbnail images, you see the url, and i want that id. any ideas?
Menew
<a href="http://www.test.ca/images/image-1.jpg?id=1" onclick="swap(this); clicked(); return false;">thumbnailimages src</a>
Menew
ah i know what the problem is.so your code works.but its not sort of doing what i want. i guess i needed to be more specific.i have thumbnail imaages. when you click on the thumbnail images, it swap the image with a small window, just like a picture veiwer: http://highslide.com/now i am passing a variable with the image, and i want that variable.i dont want the location url variable. is this clearer?
Menew
so will getting the url id make it difficult to do?
Menew
I updated the post with a script that does what you want
Gordon Tucker