How to get route parameter in JavaScript?

I have an ASP.NET website with URL rewriting enabled. I know how to get route parameters using C #:

Page.RouteData.Values["id"] as string;

But I do not know how to get it from javascript? Below is my rewritten link format:

http://www.domain.com/topic/ {id} / {title}

I want to get this {id} field using JavaScript. Please, help!

UPDATE

I have this code that receives a request parameter.

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

How can this be changed to get the route parameter?

+4
source share
1 answer
function getValueAtIndex(index){
    var str = "http://www.sample.com/234/Fiddle/test"; //window.location.href; 
    return str.split("/")[index];
}
console.log(getValueAtIndex(3));
+5
source

All Articles