-1

Possible Duplicate:
how to get GET and POST variables with JQuery?
Get query string values in JavaScript

lets say my site has url http://www.akbrowser.tk/ds/?q=http://www.chess.com&r=http://www.blackle.com

(the two parameters are URLs)

I now want to get two javascript variables on the site, with the values of the two urls. (so the first variable would be the chess.com, and the second would be blackle.com [of course it would have the http and all, but I can only post one hyperlink])

how would I do that?

I saw some other similar questions on this site, and the poster gave a long solution that I didn't understand (I think it had something to do with find a '=' and take everything after it) but in this case it would give "http:// www.chess.com&r=http://www.blackle.com [without the space]" as one of the variables.

I also saw another post with multiple parameters like mine, but the poster gave a long solution so since I didn't understand it, I couldn't really make it do what I wanted it to do.

so can someone help me?

Community
  • 1
  • 1
user1380792
  • 111
  • 2

2 Answers2

0

Try this function:

function getQueryParam(href, paramName) {
    var query = href.substring(href.indexOf('?')+1);
    var params = query.split('&');
    for(var i = 0; i < params.length; i++) {
        var param = params[i].split('=');
        if(param.length > 1) {
            if(param[0] == paramName) {
                return param[1];
            }
        }
    }
    return null;
}

console.log(getQueryParam('http://www.akbrowser.tk/ds/?q=http://www.chess.com&r=http://www.blackle.com', 'r'));
KAdot
  • 1,997
  • 13
  • 21
-1
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

 var qString = gup("q");
 var rString = gup("r");

What this does is do a regex to find whatever [name] you pass in to the function.

FlavorScape
  • 13,301
  • 12
  • 75
  • 117
  • thanks, but I still don't understand. What are the 2 variables? is it name or results or regex? – user1380792 Jun 14 '12 at 20:16
  • sounds like you need to learn to read javascript a little better based on your question. This is a function that retrieves values based on the query variable names. The result would only ever be results[1] since that's what the function returns (unless not found). – FlavorScape Jun 14 '12 at 20:40