我在网站中使用.net MVC,我需要使用javascript获取URL,但是仅获取控制器和操作名称,例如,如果我拥有:
http://stackoverflow.com/questions/9513736/current-url-without-parameters-hash-https
我需要:
http://stackoverflow.com/questions/9513736/
我发现了一些解决方案,例如:
urlBase = location.href.substring(0, location.href.lastIndexOf("/")+1)
->http://stackoverflow.com/questions/9513736/
但是,如果我的URL没有任何参数,则操作名称将被切断,如下所示:
http://stackoverflow.com/questions/
有人对此有解决方案吗?
您可以window.location.pathname
为此使用。
对于此问题中的网址,它返回
"/questions/28946447/how-to-get-only-the-controller-name-and-action-name-from-url-with-jquery"
要正确读取它,可以使用:使用以下命令window.location.pathname.split("/")
读取值:
var url = window.location.pathname.split("/");
var questions = url[1];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句