Change URL parameters

前端 未结 26 2201
孤独总比滥情好
孤独总比滥情好 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:17

    My solution:

    const setParams = (data) => {
        if (typeof data !== 'undefined' && typeof data !== 'object') {
            return
        }
    
        let url = new URL(window.location.href)
        const params = new URLSearchParams(url.search)
    
        for (const key of Object.keys(data)) {
            if (data[key] == 0) {
                params.delete(key)
            } else {
                params.set(key, data[key])
            }
        }
    
        url.search = params
        url = url.toString()
        window.history.replaceState({ url: url }, null, url)
    }
    

    Then just call "setParams" and pass an object with data you want to set.

    Example:

    $('select').on('change', e => {
        const $this = $(e.currentTarget)
        setParams({ $this.attr('name'): $this.val() })
    })
    

    In my case I had to update a html select input when it changes and if the value is "0", remove the parameter. You can edit the function and remove the parameter from the url if the object key is "null" as well.

    Hope this helps yall

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

    my function support removing param

    function updateURLParameter(url, param, paramVal, remove = false) {
            var newAdditionalURL = '';
            var tempArray = url.split('?');
            var baseURL = tempArray[0];
            var additionalURL = tempArray[1];
            var rows_txt = '';
    
            if (additionalURL)
                newAdditionalURL = decodeURI(additionalURL) + '&';
    
            if (remove)
                newAdditionalURL = newAdditionalURL.replace(param + '=' + paramVal, '');
            else
                rows_txt = param + '=' + paramVal;
    
            window.history.replaceState('', '', (baseURL + "?" + newAdditionalURL + rows_txt).replace('?&', '?').replace('&&', '&').replace(/\&$/, ''));
        }
    
    0 讨论(0)
  • 2020-11-22 09:18

    You can use this my library to do the job: https://github.com/Mikhus/jsurl

    var url = new Url('site.fwx?position=1&archiveid=5000&columns=5&rows=20&sorting=ModifiedTimeAsc');
    url.query.rows = 10;
    alert( url);
    
    0 讨论(0)
  • 2020-11-22 09:19

    you can do it via normal JS also

    var url = document.URL
    var newAdditionalURL = "";
    var tempArray = url.split("?");
    var baseURL = tempArray[0];
    var aditionalURL = tempArray[1]; 
    var temp = "";
    if(aditionalURL)
    {
    var tempArray = aditionalURL.split("&");
    for ( var i in tempArray ){
        if(tempArray[i].indexOf("rows") == -1){
                newAdditionalURL += temp+tempArray[i];
                    temp = "&";
                }
            }
    }
    var rows_txt = temp+"rows=10";
    var finalURL = baseURL+"?"+newAdditionalURL+rows_txt;
    
    0 讨论(0)
  • 2020-11-22 09:20

    I was looking for the same thing and found: https://github.com/medialize/URI.js which is quite nice :)

    -- Update

    I found a better package: https://www.npmjs.org/package/qs it also deals with arrays in get params.

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

    No library, using URL() WebAPI (https://developer.mozilla.org/en-US/docs/Web/API/URL)

    function setURLParameter(url, parameter, value) {
        let url = new URL(url);
        if (url.searchParams.get(parameter) === value) {
            return url;
        }
        url.searchParams.set(parameter, value);
        return url.href;
    }
    

    This doesn't work on IE: https://developer.mozilla.org/en-US/docs/Web/API/URL#Browser_compatibility

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