Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
180
rated 0 times [  181] [ 1]  / answers: 1 / hits: 20515  / 9 Years ago, mon, march 9, 2015, 12:00:00

I am using .net MVC in my website and I need to get the URL in javascript, but only the controller and action name, for instance if i have:



http://stackoverflow.com/questions/9513736/current-url-without-parameters-hash-https



I need:



http://stackoverflow.com/questions/9513736/



I have found some solutions such as:



urlBase = location.href.substring(0, location.href.lastIndexOf(/)+1)



->http://stackoverflow.com/questions/9513736/



However if my URL does not have any parameters I the action name is cut off like so:



http://stackoverflow.com/questions/



Does anyone have a solution for this?


More From » jquery

 Answers
17

You can use
window.location.pathname
for this.



For the url in this question it returns



/questions/28946447/how-to-get-only-the-controller-name-and-action-name-from-url-with-jquery


To read it properly you can use: window.location.pathname.split(/)

Read the value with:



var url = window.location.pathname.split(/);
var questions = url[1];

[#67512] Friday, March 6, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kiyam

Total Points: 448
Total Questions: 96
Total Answers: 92

Location: Philippines
Member since Sat, Jul 11, 2020
4 Years ago
;