Search code examples
javascripturlpathname

Get the Value after `/` from Variable


I was trying to get the current page pathname using javascript. I was able to get it, but I'm getting it with the Forward Slash /. I'd want to remove that / from variable.

Here's the code which I tried. :)

var URLPath = window.location.pathname;
alert(URLPath);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Solution

  • Use slice or substr to get a substring starting from the second character:

    var URLPath = window.location.pathname.slice(1);
    //                                    ^^^^^^^^^
    alert(URLPath);