Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

If I'm, for example, on this page

www.example.com/admin/bridge/boilerplate

What is the best way (Using plain javascript, or jQuery (Without loading another plugin) to go up one level, e.g.

www.example.com/admin/bridge

At the moment we are using

window.history.go(-1);

which interferes with submitted forms, etc. This is used normally on a function like this:

$("button.cancel").bind("click", function( e ){
    window.history.go(-1);
    e.preventDefault();
});
like image 363
Professional.GEEK Avatar asked Dec 21 '11 21:12

Professional.GEEK


3 Answers

Simple:

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('/');
like image 59
Joseph Silber Avatar answered Nov 09 '22 06:11

Joseph Silber


var i = window.location.href.lastIndexOf("/");
window.location = window.location.href.substr(0,i)
like image 39
Greg Guida Avatar answered Nov 09 '22 07:11

Greg Guida


thanks, but this 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('/');
});
like image 35
Armpc Avatar answered Nov 09 '22 06:11

Armpc