Search code examples
javascriptphpjqueryback

PHP "Go to Parent Page" Button on Directory Style URL


I was trying to find the solution throughout the web and I can't see any solution that applies to my problem.

I have a directory styled website: link here

And I also have a Back Button on every page. For now, the button sends the user back to the homepage, but what I want to do is to go back to the parent page.

For example, if the user is here "site.com/page/subpage", and they click on the back button, it should send them to "site.com/page".

I haven't found any solution to this. I'm willing to use jQuery or PHP.

I would really appreciate some help on this matter.

Thanks in advance,

Bruno

EDIT: My purpose is NOT to use the "history back", because if a user doesn't come from the parent page or if they come from any other site, it will send them back there. I don't want that, I want it to be a "go to parent page" button.

EDIT 2: Theory: My theory would be to get the whole url with:

$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

and trim it, removing the last part. For example, removing "/subpage" from "site.com/page/subpage"


Solution

  • Try (javascript):

    var url = window.location.href.split('/');
    url.splice(url.length-1,1);
    var pUrl = url.join('/');
    window.location.href = pUrl;