![](/img/trans.png)
[英]How to get the action name (or view name) in Grails from Javascript or JQuery
[英]How to get only the Controller name and action name from URL with jQuery
我在我的网站上使用.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];
window.location.pathname;
这将从网址返回path
。 然后使用split
获取控制器名称和操作名称
var path=window.location.pathname;
var abc=path.split("/");
var controller=abc[0];
var action=abc[1] || "index";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.