How to remove last / value from url in javascript

array
javascript
last value

Here we are some different types to get last value from array

function RemoveLastDirectoryPartOf(the_url)
{
    var the_arr = the_url.split('/');
    the_arr.pop();
    return( the_arr.join('/') );
}
var res = RemoveLastDirectoryPartOf(window.location.href);
alert(res);

Try this simple method using lastIndexOf() and slice()

url = window.location.href;
url = url.slice(0, url.lastIndexOf('/'));
alert(url);
url = url.slice(0, url.lastIndexOf('/'));
alert(url);

 

 

 

 

 

 

You might also like:

Functions in javascript

25-05-2018 functions javascript es5 es6 arrow functions named functions Closures Nested Functions Callbacks Arrow functions.

Time Sorting Ascending or Descending Order in Javascript

19-07-2017 Javascript ascending order descending order

How to insert bulk information into database in single query execution

18-03-2017 insert sql bulk database

How to use request module in node js

16-03-2017 request module node js

How to remove last / value from url in javascript

27-02-2017 array javascript last value