Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何删除按钮单击上的查询字符串?_Jquery_Url_Query String - Fatal编程技术网

Jquery 如何删除按钮单击上的查询字符串?

Jquery 如何删除按钮单击上的查询字符串?,jquery,url,query-string,Jquery,Url,Query String,单击按钮时如何删除url的查询字符串?我的url如下所示: www.mysite.com/?tx\u felogin\u pi1[忘记]=1 此页面显示获取新密码的表单,所有内容都在覆盖框中。当用户突然再次想起他的userdata时,我需要在框中提供一个链接以返回到signin表单(也在OverlyBox中),但我不能使用固定url,因为链接(和OverlyBox)应该可以在每个页面上访问。所以我需要一个链接,删除查询字符串,独立的网址之前 我试过这个,但不起作用: function g

单击按钮时如何删除url的查询字符串?我的url如下所示:

www.mysite.com/?tx\u felogin\u pi1[忘记]=1

此页面显示获取新密码的表单,所有内容都在覆盖框中。当用户突然再次想起他的userdata时,我需要在框中提供一个链接以返回到signin表单(也在OverlyBox中),但我不能使用固定url,因为链接(和OverlyBox)应该可以在每个页面上访问。所以我需要一个链接,删除查询字符串,独立的网址之前

我试过这个,但不起作用:

    function getPathFromUrl(url) {
      return url.split("?")[0];
    }
    // from http://stackoverflow.com/questions/2540969/remove-querystring-from-url

    $("#querystring").click(getPathFromUrl);

我的控制台中的错误是:url.split不是一个函数…

什么是
url
?您必须从
窗口获取url。位置
对象:

$("#querystring").click(function(){
    // you can use url.split("?")[0]; here, however be sure it's in global scope

});
$("#querystring").click(function(){
    window.location.href = window.location.href.split('?')[0];
});

要在不使用querystring的情况下获取url,请使用以下命令

function getPathFromUrl() {
  var url = window.location.href; 

  if(url.indexOf("?") != -1)
     url = url.split("?")[0];

  return url;
}
用法


这将从URL中删除查询字符串

假设您的url为:-

http://localhost:4567/projectName/abc.php?id=307

只需将button类替换为button的类,并对要删除查询字符串的页面运行此代码

    $(document).on('click',".button", function(){
    window.location.href = window.location.origin + window.location.pathname;    
    });
运行上述代码后,您的url将为:-
http://localhost:4567/projectName/abc.php

无需确定字符串是否包含
<代码>'test'。split('?')[0]
将返回
test
。这是正确的,但是为什么它的
split
不包含“?”?因为,虽然我没有测试它,但我怀疑
split
indexOf
更昂贵(此外,您很可能会同时运行它们)。为什么要把你的代码弄得这么复杂呢?答案是极好的+1。它会重新加载页面。
    $(document).on('click',".button", function(){
    window.location.href = window.location.origin + window.location.pathname;    
    });