Search code examples
javascriptjavajqueryjspjstl

How do I merge the current request parameters with a new parameter in JSP/JSTL/JQuery?


I have the following URL:

http://127.0.0.1:8080/admin/seller?email=tim%40example.com

I have the following JSP:

<a class="btn btn-primary ${page==pages||pages==0?'disabled':''}" 
  href="?page=${page + 1}">Next</a>

I would like to merge the current query string with page=${page+1}. It is not only just email, it could be many different GET request parameters. How is this possible in JSTL or JSP?

I could create a JQuery event handler for onclick events, but there's got to be an easier way.


Solution

  • I solved it with this, but there's got to be a better way!

    <a class="btn btn-primary ${page==pages||pages==0?'disabled':''}" href="javascript:page(${page + 1});">Next</a>
    
    <script>
    location.queryString = {};
    location.search.substr(1).split("&").forEach(function (pair) {
        if (pair === "") return;
        var parts = pair.split("=");
        location.queryString[parts[0]] = parts[1] &&
            decodeURIComponent(parts[1].replace(/\+/g, " "));
    });
    function page(p) {
        window.location.queryString.page = p;
        window.location.replace(window.location.pathname + "?" + $.param(window.location.queryString));
    }
    </script>