我试图删除文档准备浏览器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]);
});

当前回答

在Javascript中:

window.location.href =  window.location.href.split("?")[0]

其他回答

        var currURL = window.location.href;
        var url = (currURL.split(window.location.host)[1]).split("?")[0];
        window.history.pushState({}, document.title, url);

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

//Joraid code is working but i altered as below. it will work if your URL contain "?" mark or not
//replace URL in browser
if(window.location.href.indexOf("?") > -1) {
    var newUrl = refineUrl();
    window.history.pushState("object or string", "Title", "/"+newUrl );
}

function refineUrl()
{
    //get full url
    var url = window.location.href;
    //get url after/  
    var value = url = url.slice( 0, url.indexOf('?') );
    //get the part after before ?
    value  = value.replace('@System.Web.Configuration.WebConfigurationManager.AppSettings["BaseURL"]','');  
    return value;     
}

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

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

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

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

在Javascript中:

window.location.href =  window.location.href.split("?")[0]