0

我希望 javascript 变量是 ?url= 后面的 url..

例如:当前网址是

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

所以变量必须是http://www.google.com/

我试过这个,但它不起作用……为什么?

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

谢谢。

4

3 回答 3

2

我认为以下内容对您有用:

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);
于 2013-05-01T06:23:23.967 回答
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/

于 2013-05-01T06:25:53.940 回答
1

尝试这个:

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

甚至更简单:

var appname = /\?url\=(.+)/.exec(url)[1];
于 2013-05-01T06:23:32.800 回答