我试图删除文档准备浏览器url中的“?”后的所有内容。

以下是我正在尝试的:

jQuery(document).ready(function($) {

var url = window.location.href;
    url = url.split('?')[0];
});

我可以这样做,并看到下面的工作:

jQuery(document).ready(function($) {

var url = window.location.href;
    alert(url.split('?')[0]);
});

当前回答

下面是如何删除特定的查询参数(即使是重复的),而不删除其他查询参数:

const newUrl =新的URL(location.href); newUrl.searchParams.delete(“交易”); window.history。replaceState({},文档。标题、newUrl.href);

其他回答

一个简单的方法做到这一点,工作在任何页面,需要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);
   }
}
        var currURL = window.location.href;
        var url = (currURL.split(window.location.host)[1]).split("?")[0];
        window.history.pushState({}, document.title, url);

这将是一种只清除查询字符串的更干净的方式。

清除所有参数,不做页面刷新,如果你使用HTML5,那么你可以这样做:

历史。pushState({}, ", 'index.html');//将index.html替换为你的页面名称

这将在浏览器历史记录中添加一个条目。如果不想添加新条目而只想替换旧条目,还可以考虑replaceState。

这些都是误导,你永远不想添加到浏览器历史记录,除非你想在单页应用程序中进入不同的页面。如果你想在不改变页面的情况下删除参数,你必须使用:

window.history.replaceState(null, null, window.location.pathname);

更好的解决方案:

window.history.pushState(null, null, window.location.pathname);