简体   繁体   English

添加或更新查询字符串参数 - URL保持不变

[英]Add or update query string parameter - URL remains unchanged

I am using this block of code to manage query string parameters : 我正在使用此代码块来管理查询字符串参数:

function UpdateQueryString(key, value, url) {
    if (!url) url = window.location.href;
    var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi");

    if (re.test(url)) {
        if (typeof value !== 'undefined' && value !== null)
            return url.replace(re, '$1' + key + "=" + value + '$2$3');
        else {
            var hash = url.split('#');
            url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
            if (typeof hash[1] !== 'undefined' && hash[1] !== null) 
                url += '#' + hash[1];
            return url;
        }
    }
    else {
        if (typeof value !== 'undefined' && value !== null) {
            var separator = url.indexOf('?') !== -1 ? '&' : '?',
                hash = url.split('#');
            url = hash[0] + separator + key + '=' + value;
            if (typeof hash[1] !== 'undefined' && hash[1] !== null) 
                url += '#' + hash[1];
            return url;
        }
        else
            return url;
    }
}

Function source : add or update query string parameter 功能源: 添加或更新查询字符串参数

I am calling the function, everything runs fine but the url on my browser is not changed. 我正在调用该函数,一切运行正常但我的浏览器上的url没有更改。 I used also Chrome console to check if the function was correctly called and the function is ok and retuns the expected value but the url remains unchanged. 我还使用Chrome控制台来检查功能是否被正确调用并且功能正常并且返回预期值但是网址保持不变。

Thanks for your help 谢谢你的帮助

This function is not changing the url. 此功能不会更改网址。 It's simply returning a string. 它只是返回一个字符串。

If you wanna change the url (reloading the page) you should write something like this: 如果你想更改网址(重新加载页面),你应该写这样的东西:

window.location.href = UpdateQueryString(your_key, your_value, your_url)

If you wanna change the url without reloading the entire page, using the HTML5 History/State APIs, you might be interested in this: 如果您想在不重新加载整个页面的情况下更改URL,使用HTML5历史/状态API,您可能会对此感兴趣:

https://github.com/browserstate/history.js/ https://github.com/browserstate/history.js/

the point is this function doesn't change the url, you should do it by yourself like: 关键是这个函数不会改变网址,你应该自己做,如:

 window.location.href = UpdateQueryString("yourkey","newvalue");

or change it like: 或改变它:

function UpdateQueryString(key, value, url) {
    //this flag is to check if you want to change the current page url
    var iscurrentpage = false;
    if (!url){
        url = window.location.href;
        iscurrentpage = true;
    }
    var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi");

    //this variable would be used as the result for this function
    var result = null;

    if (re.test(url)) {
        if (typeof value !== 'undefined' && value !== null) result = url.replace(re, '$1' + key + "=" + value + '$2$3');
        else {
            var hash = url.split('#');
            url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
            if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
            result = url;
        }
    } else {
        if (typeof value !== 'undefined' && value !== null) {
            var separator = url.indexOf('?') !== -1 ? '&' : '?',
                hash = url.split('#');
            url = hash[0] + separator + key + '=' + value;
            if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
            result = url;
        } else result = url;
    }
    //if this is current page update the current page url
    if(iscurrentpage) window.location.href = result;
    return result;
}

then call it without the url parameter, just with 2 parameter: 然后在没有url参数的情况下调用它,只需使用2个参数:

UpdateQueryString("yourkey", "newvalue");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM