使用JavaScript检查URL是否传递变量

时间:2013-09-25 03:55:21

标签: javascript url variables parameters

如何检查URL是否包含变量?我有这样的功能

我的第二个问题是,让我们说URL已经传递了lang变量,就像这样(http://index.php?id=23&lang=en)我希望当它们再次运行这个函数时它会将lang变量替换为new而不是add这样的新lang变量(http://index.php?id=23&lang=en&lang=jp

function menu_goto(newvalue)
{
var baseurl = window.location.href ;

var langwithpara = "&lang=" + newvalue;
var langwopara = "?lang=" + newvalue;

    if (newvalue != 0) {
        if(baseurl.match(/?/)){
        alert ('123');
        location.href = baseurl + langwithpara ;
        }
        else{
        alert ('456');
        location.href = baseurl + langwopara ;
        }
    }
}

我的新编码(工作)

function menu_goto(newvalue)
{
  var baseurl = window.location.href ;
  var url = baseurl + ( (baseurl.match(/\?/))? '&':'?' ) + 'lang=' + newvalue;

  location.href = url ;
}

4 个答案:

答案 0 :(得分:0)

您只需使用indexOf()方法即可

if(window.location.href.indexOf("your value") != -1) {
     alert("It contains");
 }
 else {
    alert("Nope");
 }

答案 1 :(得分:0)

function menu_goto(newvalue)
        {
        var baseurl = window.location.href ;
        var langwithpara = "&lang=" + newvalue;
        var langwopara = "?lang=" + newvalue;

            if (newvalue != 0) {
                if(baseurl.match(/\?/)){ // change /?/ to /\?/
                alert('123');
                location.href = baseurl + langwithpara ;
                }
                else{
                alert('456');
                location.href = baseurl + langwopara ;
                }
            }
        }

答案 2 :(得分:0)

问题可能是失去了"?"在你的RegExp中:

if (baseurl.match(/\?/)) { // ...

稍微短一些:

var url = baseurl + ( (baseurl.match(/\?/))? '&':'?' ) + 'lang=' + newvalue;

你可能想要清理任何param" lang"之前,所以它不会多次出现。

最好像

那样重新组装网址
function menu_goto(newvalue) {
    var params = {};
    if (self.location.search) {
      var pairs = self.location.search.substring(1).split("&"); // skip the "?"
      for (var i = 0; i < pairs.length; i++) {
        var parts = pairs[i].split('=');
        params[parts[0]] = parts[1];
      }
    }
    params.lang = newvalue;
    var query = new Array();
    for (var p in params) query.push(p + '=' + params[p]);
    var url = self.location.protocol + '//' + self.location.host + self.location.pathname
              + '?' + query.join('&');

    self.location.href = url;
}

这是使用RegExps的另一种解决方案:

function menu_goto2(newvalue) {
    var url = self.location.href;
    url = url.replace(/#.*/, ''); // clean any hash at end
    url = url.replace(/[&\?]lang=[^&]*/, ''); // clean any param lang and its value
    // we might have had two or more params and "lang" was the first one
    // then we might have lost the "?" => replace first "&" by "?"
    if (url.indexOf('?') < 0 && url.indexOf('&') >= 0) url = url.replace(/&/, '?');
    url += ( url.match(/\?/)? '&':'?') + 'lang=' + newvalue; // add the new param lang
    self.location.href = url;
}

可以缩短为

function menu_goto3(newvalue) {
    var url = self.location.href.replace(/#.*/, '').replace(/[&\?]lang=[^&]*/, '');
    if (url.indexOf('?') < 0 && url.indexOf('&') >= 0) url = url.replace(/&/, '?');
    url += ( url.match(/\?/)? '&':'?') + 'lang=' + newvalue;
    self.location.href = url;
}

答案 3 :(得分:0)

window.location实际上是一个对象,它有一个'search'属性,可以更容易地解析查询字符串。

function getParam(param){
    var qs = window.location.search.substring(1).split('&');
    var qsp;
    for (p in qs){
        qsp = qs[p].split('=');
        if (qsp[0] == param) return qsp[1]; 
    }
    return null;
}

检查特定参数:

var value = getParam('name');