append to url and refresh page

后端 未结 11 2127
终归单人心
终归单人心 2020-12-07 14:07

I am looking to write a piece of javascript that will append a parameter to the current url and then refresh the page - how can I do this?

相关标签:
11条回答
  • 2020-12-07 14:58

    Please check the below code :

    /*Get current URL*/    
    var _url = location.href; 
    /*Check if the url already contains ?, if yes append the parameter, else add the parameter*/
    _url = ( _url.indexOf('?') !== -1 ) ? _url+'&param='+value : _url+'?param='+value;
    /*reload the page */
    window.location.href = _url;
    
    0 讨论(0)
  • 2020-12-07 15:04

    Most of the answers here suggest that one should append the parameter(s) to the URL, something like the following snippet or a similar variation:

    location.href = location.href + "&parameter=" + value;
    

    This will work quite well for the majority of the cases.

    However

    That's not the correct way to append a parameter to a URL in my opinion.

    Because the suggested approach does not test if the parameter is already set in the URL, if not careful one may end up with a very long URL with the same parameter repeated multiple times. ie:

    https://stackoverflow.com/?&param=1&param=1&param=1&param=1&param=1&param=1&param=1&param=1&param=1
    

    at this point is where problems begin. The suggested approach could and will create a very long URL after multiple page refreshes, thus making the URL invalid. Follow this link for more information about long URL What is the maximum length of a URL in different browsers?

    This is my suggested approach:

    function URL_add_parameter(url, param, value){
        var hash       = {};
        var parser     = document.createElement('a');
    
        parser.href    = url;
    
        var parameters = parser.search.split(/\?|&/);
    
        for(var i=0; i < parameters.length; i++) {
            if(!parameters[i])
                continue;
    
            var ary      = parameters[i].split('=');
            hash[ary[0]] = ary[1];
        }
    
        hash[param] = value;
    
        var list = [];  
        Object.keys(hash).forEach(function (key) {
            list.push(key + '=' + hash[key]);
        });
    
        parser.search = '?' + list.join('&');
        return parser.href;
    }
    

    With this function one just will have to do the following:

    location.href = URL_add_parameter(location.href, 'param', 'value');
    
    0 讨论(0)
  • 2020-12-07 15:06

    Shorter than the accepted answer, doing the same, but keeping it simple:

    window.location.search += '&param=42';
    

    We don't have to alter the entire url, just the query string, known as the search attribute of location.

    When you are assigning a value to the search attribute, the question mark is automatically inserted by the browser and the page is reloaded.

    0 讨论(0)
  • 2020-12-07 15:08

    Try this

    var url = ApiUrl(`/customers`);
      if(data){
       url += '?search='+data; 
      }
      else
      { 
          url +=  `?page=${page}&per_page=${perpage}`;
      }
      console.log(url);
    
    0 讨论(0)
  • 2020-12-07 15:10

    this should work (not tested!)

    var url = window.location.href;    
    if (url.indexOf('?') > -1){
       url += '&param=1'
    }else{
       url += '?param=1'
    }
    window.location.href = url;
    
    0 讨论(0)
提交回复
热议问题