4

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.

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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

6 Comments

i got undefined upon onclick hmmm
made the change but still says undefined..hmmm
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?
<a href="test.ca/images/image-1.jpg?id=1" onclick="swap(this); clicked(); return false;">thumbnailimages src</a>
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: 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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.