Skip to content Skip to sidebar Skip to footer

Filename From Url Excluding Querystring

I have a url : http://www.xyz.com/a/test.jsp?a=b&c=d How do I get test.jsp of it ?

Solution 1:

This should do it:

var path = document.location.pathname,
    file = path.substr(path.lastIndexOf('/'));

Reference:document.location, substr, lastIndexOf

Solution 2:

I wont just show you the answer, but I'll give you direction to it. First... strip out everything after the "?" by using a string utility and location.href.status (that will give you the querystring). Then what you will be left with will be the URL; get everything after the last "/" (hint: lastindexof).

Solution 3:

Use a regular expression.

var urlVal = 'http://www.xyz.com/a/test.jsp?a=b&c=d'; var result = /a\/(.*)\?/.exec(urlVal)[1]

the regex returns an array, use [1] to get the test.jsp

Solution 4:

This method does not depend on pathname:

<script>var url = 'http://www.xyz.com/a/test.jsp?a=b&c=d';
        var file_with_parameters = url.substr(url.lastIndexOf('/') + 1);
        var file = file_with_parameters.substr(0, file_with_parameters.lastIndexOf('?')); 
        // file now contains "test.jsp"</script>

Solution 5:

var your_link = "http://www.xyz.com/a/test.jsp?a=b&c=d";

// strip the query from the link
your_link = your_link.split("?");
your_link = your_link[0];

// get the the test.jsp or whatever is therevar the_part_you_want = your_link.substring(your_link.lastIndexOf("/")+1);

Post a Comment for "Filename From Url Excluding Querystring"