Updating existing URL querystring values with jQuery

前端 未结 9 761
逝去的感伤
逝去的感伤 2020-12-01 16:15

Let\'s say I have a url such as:

http://www.example.com/hello.png?w=100&h=100&bg=white

What I\'d like to do is update the values of

相关标签:
9条回答
  • 2020-12-01 16:29

    Another way (independent of jQuery or other libraries): https://github.com/Mikhus/jsurl

    var u = new Url;
    // or
    var u = new Url('/some/path?foo=baz');
    alert(u);
    u.query.foo = 'bar';
    alert(u);
    
    0 讨论(0)
  • 2020-12-01 16:33

    My URL is like this: http://localhost/dentistryindia/admin/hospital/add_procedure?hid=241&hpr_id=12

    var reExp = /hpr_id=\\d+/;
    var url = window.location.href;
    url = url.toString(); 
    var hrpid = $("#category :selected").val(); //new value to replace hpr_id
    var reExp = new RegExp("[\\?&]" + 'hpr_id' + "=([^&#]*)"),
    delimeter = reExp.exec(url)[0].charAt(0),
    newUrl = url.replace(reExp, delimeter + 'hpr_id' + "=" + hrpid);
    window.location.href = newUrl;
    

    This is how it worked for me.

    0 讨论(0)
  • 2020-12-01 16:43

    URI.js seems like the best approach to me http://medialize.github.io/URI.js/

    let uri = URI("http://test.com/foo.html").addQuery("a", "b");
    // http://test.com/foo.html?a=b
    uri.addQuery("c", 22);
    // http://test.com/foo.html?a=b&c=22
    uri.hash("h1");
    // http://test.com/foo.html?a=b&c=22#h1
    uri.hash("h2");
    // http://test.com/foo.html?a=b&c=22#h2
    
    0 讨论(0)
  • 2020-12-01 16:48

    Get query string values this way and use $.param to rebuild query string

    UPDATE:

    This is an example, also check fiddle:

      function getQueryVariable(url, variable) {
      	 var query = url.substring(1);
         var vars = query.split('&');
         for (var i=0; i<vars.length; i++) {
              var pair = vars[i].split('=');
              if (pair[0] == variable) {
                return pair[1];
              }
         }
    
         return false;
      }
    
      var url = 'http://www.example.com/hello.png?w=100&h=100&bg=white';
           
      var w = getQueryVariable(url, 'w');
      var h = getQueryVariable(url, 'h');
      var bg = getQueryVariable(url, 'bg');
    
      // http://www.example.com/hello.png?w=200&h=200&bg=white
      var params = { 'w':200, 'h':200, 'bg':bg };
      var new_url = 'http://www.example.com/hello.png?' + jQuery.param(params);

    You can change the function to use current url:

      function getQueryVariable(variable) {
      	 var query = window.location.search.substring(1);
         var vars = query.split('&');
         for (var i=0; i<vars.length; i++) {
              var pair = vars[i].split('=');
              if (pair[0] == variable) {
                return pair[1];
              }
         }
    
         return false;
      }

    0 讨论(0)
  • 2020-12-01 16:50

    Simple solution

    You can use URLSearchParams.set() like below:

    var currentUrl = 'http://www.example.com/hello.png?w=100&h=100&bg=white';
    var url = new URL(currentUrl);
    url.searchParams.set("w", "200"); // setting your param
    var newUrl = url.href; 
    console.log(newUrl);
    

    Online demo (jsfiddle)

    0 讨论(0)
  • 2020-12-01 16:52
    //update URL Parameter
    function updateURL(key,val){
        var url = window.location.href;
        var reExp = new RegExp("[\?|\&]"+key + "=[0-9a-zA-Z\_\+\-\|\.\,\;]*");
    
        if(reExp.test(url)) {
            // update
            var reExp = new RegExp("[\?&]" + key + "=([^&#]*)");
            var delimiter = reExp.exec(url)[0].charAt(0);
            url = url.replace(reExp, delimiter + key + "=" + val);
        } else {
            // add
            var newParam = key + "=" + val;
            if(!url.indexOf('?')){url += '?';}
    
            if(url.indexOf('#') > -1){
                var urlparts = url.split('#');
                url = urlparts[0] +  "&" + newParam +  (urlparts[1] ?  "#" +urlparts[1] : '');
            } else {
                url += "&" + newParam;
            }
        }
        window.history.pushState(null, document.title, url);
    }
    
    0 讨论(0)
提交回复
热议问题