Go up one "folder" or segment in jQuery / Javascript

If I, for example, on this page

www.example.com/admin/bridge/boilerplate 

What is the best way (using simple javascript or jQuery (without loading another plugin) to go up one level, like

 www.example.com/admin/bridge 

We are currently using

 window.history.go(-1); 

which interferes with the submitted forms, etc. This is usually used for such a function:

 $("button.cancel").bind("click", function( e ){ window.history.go(-1); e.preventDefault(); }); 
+4
source share
4 answers

Plain:

 var url = window.location.href; if (url.substr(-1) == '/') url = url.substr(0, url.length - 2); url = url.split('/'); url.pop(); window.location = url.join('/'); 
+9
source
 var i = window.location.href.lastIndexOf("/"); window.location = window.location.href.substr(0,i) 
+1
source

thanks, but this has been edited:

 $("#back a").click(function() { var url = window.location.href; if (url.substr(-1) == '/') url = url.substr(0, url.length - 2); url = url.split('/'); url.pop(); window.location = url.join('/'); }); 
+1
source

window.history actually manipulates what in the history of your browser is what the back / forward buttons in the browser use.

So, if the only way to get to the directory you are in is to go to it by going through the parent directories, this will work. However, this is never the only way to get to the page. Users can always add bookmarks to a page.

0
source

Source: https://habr.com/ru/post/1387466/


All Articles