Add Query String parameter to current URL using JavaScript and jQuery

Last Reply 8 months ago By dharmendr

Posted 8 months ago

hello,

i use this code to pass value to query string it works fine. the problem is i already have query string value and i want to add another query strin like

url.com?Orderid=0001   this i already have i want to add &status="active" through this javascript

currently when i use this below javascript it removes the orderid=001 query string and then add status="active" i want to keep the orderid=001 and add &status="Active" query string please advice

     <script type="text/javascript">
         $(function () {
             $('[id*=LinkButton1]').on('click', function () {
                 var type ="Active";
                 var oldURL = window.location.protocol + "//" + window.location.host + window.location.pathname;
                 var newUrl = oldURL + "?status=" + type;
                 if (window.history != 'undefined' && window.history.pushState != 'undefined') {
                     window.history.pushState({ path: newUrl }, '', newUrl);
                }
                return false;
            });
        });
    </script>
You are viewing reply posted by: dharmendr 8 months ago.
Posted 8 months ago

Hi nauna,

Refer below code.

HTML

<asp:LinkButton ID="LinkButton1" Text="Change Query String" runat="server" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('[id*=LinkButton1]').on('click', function () {
            var oldURL = window.location.href;
            var type = "Active";
            if (history.pushState) {
                var newUrl = oldURL + "&status=" + type;
                window.history.pushState({ path: newUrl }, '', newUrl);
            }
            return false;
        });
    });
</script>