Return to previous category from single page

I have next code on my single page.

  // RETURN TO PREVIOUS CATEGORY
  $('.cross').on('click', function(){
    var toPrevPage = $(this);
        toPrevPage = history.back();
  });

When you go to single page through urlOfsite/ => urlOfsite/category => urlOfsite/category/singlePage and then click on cross – you will be returned to the category from which you came, but when you go to single page through direct link – the cross does not return to the previous category.
Why does this happen and how can I fix it?

Read More

live example

Related posts

Leave a Reply

1 comment

  1. This will work for you:

    $('.cross').on('click', function(){
        var url_parts = window.location.href.split('/');
        //remove last one part
        url_parts.pop();
        // redirect to top level of category
        window.location.href = url_parts.join('/');
    });