Change URL parameters

前端 未结 26 2186
孤独总比滥情好
孤独总比滥情好 2020-11-22 08:40

I have this URL:

site.fwx?position=1&archiveid=5000&columns=5&rows=20&sorting=ModifiedTimeAsc

what I need is to be able to

相关标签:
26条回答
  • 2020-11-22 09:02

    I wrote a little helper function that works with any select. All you need to do is add the class "redirectOnChange" to any select element, and this will cause the page to reload with a new/changed querystring parameter, equal to the id and value of the select, e.g:

    <select id="myValue" class="redirectOnChange"> 
        <option value="222">test222</option>
        <option value="333">test333</option>
    </select>
    

    The above example would add "?myValue=222" or "?myValue=333" (or using "&" if other params exist), and reload the page.

    jQuery:

    $(document).ready(function () {
    
        //Redirect on Change
        $(".redirectOnChange").change(function () {
            var href = window.location.href.substring(0, window.location.href.indexOf('?'));
            var qs = window.location.href.substring(window.location.href.indexOf('?') + 1, window.location.href.length);
            var newParam = $(this).attr("id") + '=' + $(this).val();
    
            if (qs.indexOf($(this).attr("id") + '=') == -1) {
                if (qs == '') {
                    qs = '?'
                }
                else {
                    qs = qs + '&'
                }
                qs = qs + newParam;
    
            }
            else {
                var start = qs.indexOf($(this).attr("id") + "=");
                var end = qs.indexOf("&", start);
                if (end == -1) {
                    end = qs.length;
                }
                var curParam = qs.substring(start, end);
                qs = qs.replace(curParam, newParam);
            }
            window.location.replace(href + '?' + qs);
        });
    });
    
    0 讨论(0)
  • 2020-11-22 09:02

    Another variation on Sujoy's answer. Just changed the variable names & added a namespace wrapper:

    window.MyNamespace = window.MyNamespace  || {};
    window.MyNamespace.Uri = window.MyNamespace.Uri || {};
    
    (function (ns) {
    
        ns.SetQueryStringParameter = function(url, parameterName, parameterValue) {
    
            var otherQueryStringParameters = "";
    
            var urlParts = url.split("?");
    
            var baseUrl = urlParts[0];
            var queryString = urlParts[1];
    
            var itemSeparator = "";
            if (queryString) {
    
                var queryStringParts = queryString.split("&");
    
                for (var i = 0; i < queryStringParts.length; i++){
    
                    if(queryStringParts[i].split('=')[0] != parameterName){
    
                        otherQueryStringParameters += itemSeparator + queryStringParts[i];
                        itemSeparator = "&";
                    }
                }
            }
    
            var newQueryStringParameter = itemSeparator + parameterName + "=" + parameterValue;
    
            return baseUrl + "?" + otherQueryStringParameters + newQueryStringParameter;
        };
    
    })(window.MyNamespace.Uri);
    

    Useage is now:

    var changedUrl = MyNamespace.Uri.SetQueryStringParameter(originalUrl, "CarType", "Ford");
    
    0 讨论(0)
  • 2020-11-22 09:09

    2020 Solution: sets the variable or removes iti if you pass null or undefined to the value.

    var setSearchParam = function(key, value) {
        if (!window.history.pushState) {
            return;
        }
    
        if (!key) {
            return;
        }
    
        var url = new URL(window.location.href);
        var params = new window.URLSearchParams(window.location.search);
        if (value === undefined || value === null) {
            params.delete(key);
        } else {
            params.set(key, value);
        }
    
        url.search = params;
        url = url.toString();
        window.history.replaceState({url: url}, null, url);
    }
    
    0 讨论(0)
  • 2020-11-22 09:11

    This is the modern way to change URL parameters:

    function setGetParam(key,value) {
      if (history.pushState) {
        var params = new URLSearchParams(window.location.search);
        params.set(key, value);
        var newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' + params.toString();
        window.history.pushState({path:newUrl},'',newUrl);
      }
    }
    
    0 讨论(0)
  • 2020-11-22 09:12

    If you want to change the url in address bar:

    const search = new URLSearchParams(location.search);
    search.set('rows', 10);
    location.search = search.toString();
    

    Note, changing location.search reloads the page.

    0 讨论(0)
  • 2020-11-22 09:13

    URL query parameters can be easily modified using URLSearchParams and History interfaces:

    // Construct URLSearchParams object instance from current URL querystring.
    var queryParams = new URLSearchParams(window.location.search);
     
    // Set new or modify existing parameter value. 
    //queryParams.set("myParam", "myValue");
    queryParams.set("rows", "10");
     
    // Replace current querystring with the new one.
    history.replaceState(null, null, "?"+queryParams.toString());
    

    Alternatively instead of modifying current history entry using replaceState() we can use pushState() method to create a new one:

    history.pushState(null, null, "?"+queryParams.toString());
    

    https://zgadzaj.com/development/javascript/how-to-change-url-query-parameter-with-javascript-only

    0 讨论(0)
提交回复
热议问题