How can I add or update a query string parameter?

后端 未结 27 2874
别那么骄傲
别那么骄傲 2020-11-22 02:35

With javascript how can I add a query string parameter to the url if not present or if it present, update the current value? I am using jquery for my client side development

27条回答
  •  长发绾君心
    2020-11-22 03:22

    To give an code example for modifying window.location.search as suggested by Gal and tradyblix:

    var qs = window.location.search || "?";
    var param = key + "=" + value; // remember to URI encode your parameters
    if (qs.length > 1) {
        // more than just the question mark, so append with ampersand
        qs = qs + "&";
    }
    qs = qs + param;
    window.location.search = qs;
    

提交回复
热议问题