如何使用javascript/jquery从网址中删除获取变量和文件名? [英] How do I remove get variables and filename from URL using javascript/jquery?

查看:47
本文介绍了如何使用javascript/jquery从网址中删除获取变量和文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在调查此问题,但找不到针对此特定目的的可靠答案.假设我的网址是...

I was looking into this issue but I couldn't find any solid answers for this specific purpose. Let's say I have a URL of...

http://mysite.com/stuff/index.php?search=我的搜索

我如何获取此URL并从中删除index.php?search = my + search,因此它只是 http ://mysite.com/stuff/?基本上,我只想获取没有文件名的父URL或获取变量...无论使用什么URL(因此我不必为要在其上使用它的每个页面自定义功能)

How can I grab this URL and remove index.php?search=my+search from it so it would just be http://mysite.com/stuff/ ? Basically I just want to grab the parent URL without a filename or get variables... No matter what the URL (so I don't have to customize the function for every page I want to use it on)

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

So for an additional example, if it were just...

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

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

有人能帮我解决这个问题吗?我完全迷路了.

Can anyone give me a hand in figuring this out? I'm completely lost.

推荐答案

尝试使用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/"

这篇关于如何使用javascript/jquery从网址中删除获取变量和文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆