Remove URL parameters without refreshing page

前端 未结 13 973
执念已碎
执念已碎 2020-12-07 08:00

I am trying to remove everything after the \"?\" in the browser url on document ready.

Here is what I am trying:

jQuery(document).ready(function($)          


        
13条回答
  •  粉色の甜心
    2020-12-07 08:21

    a simple way to do this, works on any page, requires HTML 5

    // get the string following the ?
    var query = window.location.search.substring(1)
    
    // is there anything there ?
    if(query.length) {
       // are the new history methods available ?
       if(window.history != undefined && window.history.pushState != undefined) {
            // if pushstate exists, add a new state to the history, this changes the url without reloading the page
    
            window.history.pushState({}, document.title, window.location.pathname);
       }
    }
    

提交回复
热议问题