0

How can I check if URL contains variable or not? I have my function like this

My 2nd problem is, let says the URL already pass the lang variable, something like this (http://index.php?id=23&lang=en) i want that when they run this function again it will replace the lang variable to the new on instead of add new lang variable like this (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 ;
        }
    }
}

My new coding (work)

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

  location.href = url ;
}
4

4 回答 4

1

window.location is actually an object, it has a 'search' property that make it easier to parse the query string.

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

to check for a specific parameter :

var value = getParam('name');
于 2013-09-25T04:07:11.580 回答
0

You can simply use indexOf() method for this

if(window.location.href.indexOf("your value") != -1) {
     alert("It contains");
 }
 else {
    alert("Nope");
 }
于 2013-09-25T04:03:52.133 回答
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 ;
                }
            }
        }
于 2013-09-25T04:05:34.390 回答
0

the problem is probably the missing escaping of "?" in your RegExp:

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

a bit shorter would be:

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

You would probably want to clean any param "lang" before, so it doesn't become an array by multiple occurrence.

It would probably better to assemble the url anew like

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

Here is yet another solution using 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;
}

Which could be shortened to

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;
}
于 2013-09-25T04:05:52.910 回答