Search code examples
javascriptjqueryasp.net-mvc-3requestrawurl

Alternate for Request.RawUrl in javascript/jquery?


I was using a Request.RawUrl to get the URL after localhost. i.e if my url is localhost:52482/something/somethingelse, then this used to return /something/somethingelse.

I accessed this in a base controller(MVC3).But i found out that if there are some entries made in the page ,say some textbox and other selections, this would return a query string kind of value. How do I solve this?

I was thinking about perform this operation in javascript to see if i can get just the URL without query string. Let me know how should I proceed here?

Thanks, Adarsh


Solution

  • http://www.w3schools.com/jsref/obj_location.asp

    // testsite.com/test.html?q=blah
    
    var urlWithoutQS = window.location.host + window.location.pathname
    
    alert(urlWithoutQS);                // testsite.com/test.html
    alert(urlWithoutQS.split('/')[0]);  // testsite.com
    alert(urlWithoutQS.split('/')[1]);  // test.html