0

I want a javascript variable to be what is behind the ?url= in the url..

for example: The current url is

http://mywebsite.com/test/index.html?url=http://www.google.com/

So the variable has to be http://www.google.com/ .

I tried this, but it doesn't work… why ?

    var url = document.URL ;
    var appname = url.match(?url=(.+))[1];

Thanks.

2
  • Use location.search Commented May 1, 2013 at 6:18
  • Should the URL parameter be allowed to contain GET args? Commented May 1, 2013 at 6:31

3 Answers 3

2

I think the following will work for you:

function querystring(key) {
    var query = window.location.search.substring(1);
    var keys = query.split("&");
    for (i = 0; i < keys.length; i++) {
        var values = keys[i].split("=");
        if (values[0] == key) {
            return values[1];
        }
    }
}
var appname = querystring("url");
alert(appname);
Sign up to request clarification or add additional context in comments.

1 Comment

This is way too complicated.
1

Try this:

var regex = /\?url\=(.+)/;
var appname = regex.exec(url)[1];

or even simpler:

var appname = /\?url\=(.+)/.exec(url)[1];

Comments

1
var url = location.search.match(/url=([^&]+)&*.*$/)[1]; // http://www.google.com/

location     //location object
 .search     //the search part in location
 .match      //return string according to regex given
  [1]        //second result (result in parenthesis)

//--------Use in a function---------

function getQuery(txt){
    var result = location.search.match(new RegExp(txt + "=([^&]+)&*.*$"));
    return result === null ? undefined : result[1];
}

http://jsfiddle.net/DerekL/J4FfZ/

Comments

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.