Adding a parameter to the URL with JavaScript

后端 未结 30 2601
刺人心
刺人心 2020-11-22 07:33

In a web application that makes use of AJAX calls, I need to submit a request but add a parameter to the end of the URL, for example:

Original URL:

30条回答
  •  故里飘歌
    2020-11-22 08:09

    Following function will help you to add,update and delete parameters to or from URL.

    //example1and

    var myURL = '/search';
    
    myURL = updateUrl(myURL,'location','california');
    console.log('added location...' + myURL);
    //added location.../search?location=california
    
    myURL = updateUrl(myURL,'location','new york');
    console.log('updated location...' + myURL);
    //updated location.../search?location=new%20york
    
    myURL = updateUrl(myURL,'location');
    console.log('removed location...' + myURL);
    //removed location.../search
    

    //example2

    var myURL = '/search?category=mobile';
    
    myURL = updateUrl(myURL,'location','california');
    console.log('added location...' + myURL);
    //added location.../search?category=mobile&location=california
    
    myURL = updateUrl(myURL,'location','new york');
    console.log('updated location...' + myURL);
    //updated location.../search?category=mobile&location=new%20york
    
    myURL = updateUrl(myURL,'location');
    console.log('removed location...' + myURL);
    //removed location.../search?category=mobile
    

    //example3

    var myURL = '/search?location=texas';
    
    myURL = updateUrl(myURL,'location','california');
    console.log('added location...' + myURL);
    //added location.../search?location=california
    
    myURL = updateUrl(myURL,'location','new york');
    console.log('updated location...' + myURL);
    //updated location.../search?location=new%20york
    
    myURL = updateUrl(myURL,'location');
    console.log('removed location...' + myURL);
    //removed location.../search
    

    //example4

    var myURL = '/search?category=mobile&location=texas';
    
    myURL = updateUrl(myURL,'location','california');
    console.log('added location...' + myURL);
    //added location.../search?category=mobile&location=california
    
    myURL = updateUrl(myURL,'location','new york');
    console.log('updated location...' + myURL);
    //updated location.../search?category=mobile&location=new%20york
    
    myURL = updateUrl(myURL,'location');
    console.log('removed location...' + myURL);
    //removed location.../search?category=mobile
    

    //example5

    var myURL = 'https://example.com/search?location=texas#fragment';
    
    myURL = updateUrl(myURL,'location','california');
    console.log('added location...' + myURL);
    //added location.../search?location=california#fragment
    
    myURL = updateUrl(myURL,'location','new york');
    console.log('updated location...' + myURL);
    //updated location.../search?location=new%20york#fragment
    
    myURL = updateUrl(myURL,'location');
    console.log('removed location...' + myURL);
    //removed location.../search#fragment
    

    Here is the function.

    function updateUrl(url,key,value){
          if(value!==undefined){
            value = encodeURI(value);
          }
          var hashIndex = url.indexOf("#")|0;
          if (hashIndex === -1) hashIndex = url.length|0;
          var urls = url.substring(0, hashIndex).split('?');
          var baseUrl = urls[0];
          var parameters = '';
          var outPara = {};
          if(urls.length>1){
              parameters = urls[1];
          }
          if(parameters!==''){
            parameters = parameters.split('&');
            for(k in parameters){
              var keyVal = parameters[k];
              keyVal = keyVal.split('=');
              var ekey = keyVal[0];
              var evalue = '';
              if(keyVal.length>1){
                  evalue = keyVal[1];
              }
              outPara[ekey] = evalue;
            }
          }
    
          if(value!==undefined){
            outPara[key] = value;
          }else{
            delete outPara[key];
          }
          parameters = [];
          for(var k in outPara){
            parameters.push(k + '=' + outPara[k]);
          }
    
          var finalUrl = baseUrl;
    
          if(parameters.length>0){
            finalUrl += '?' + parameters.join('&'); 
          }
    
          return finalUrl + url.substring(hashIndex); 
      }
    

提交回复
热议问题