How can I add or update a query string parameter?

后端 未结 27 3203
别那么骄傲
别那么骄傲 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:31

    window.location.search is read/write.

    However - modifying the query string will redirect the page you're on and cause a refresh from the server.

    If what you're attempting to do is maintain client side state (and potentially make it bookmark-able), you'll want to modify the URL hash instead of the query string, which keeps you on the same page (window.location.hash is read/write). This is how web sites like twitter.com do this.

    You'll also want the back button to work, you'll have to bind javascript events to the hash change event, a good plugin for that is http://benalman.com/projects/jquery-hashchange-plugin/

提交回复
热议问题