如何使用 javascript/jquery 从 URL 中删除获取变量和文件名?

2024-01-09

我正在研究这个问题,但找不到针对此特定目的的任何可靠答案。假设我有一个网址...

http://mysite.com/stuff/index.php?search=my+search http://mysite.com/stuff/index.php?search=my+search

我怎样才能抓取这个URL并从中删除index.php?search=my+search,这样它就可以了http://mysite.com/stuff/ http://mysite.com/stuff/?基本上我只想获取没有文件名的父 URL 或获取变量...无论 URL 是什么(所以我不必为我想使用它的每个页面自定义该函数)

再举一个例子,如果只是......

http://mysite.com/silly.php?hello=ahoy http://mysite.com/silly.php?hello=ahoy

我只想返回根http://mysite.com http://mysite.com

谁能帮我解决这个问题吗?我完全迷路了。


尝试使用lastIndexOf("/"):

var url = "http://mysite.com/stuff/index.php?search=my+search";
url = url.substring(0, url.lastIndexOf("/") + 1);
alert(url); // it will be "http://mysite.com/stuff/"

OR

var url = "http://mysite.com/silly.php?hello=ahoy";
url = url.substring(0, url.lastIndexOf("/") + 1);
alert(url); // it will be "http://mysite.com/"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 javascript/jquery 从 URL 中删除获取变量和文件名? 的相关文章

随机推荐