A
A
askeer2016-10-12 18:39:29
JavaScript
askeer, 2016-10-12 18:39:29

How to make the back button work within a specific site?

Hello.
Please help to finish the js code so that it does exactly what is intended.
Essence of the question.
There is a button when clicking on which we return back (to the previous page), how to make this jsworked within a specific domain, that is, by going to example.com/page/1 from the search results, or directly from the browser start page, we would not be kicked back when we clicked the "Back" button on the issue page or on the browser start page . How to make the code understand that we came to the example.com/page/1 page from another resource and when you click on the "Back" button, it would send us to example.com and not the issue page, for example? + if we go to the example.com/page/1 page, for example from the example.com/page/ page, then when we click on the "Back" button, we would be returned to the example.com/page page.
How to make the "Back" button work within a specific site? If we came to the page example.com/page/1 from the page example.com/page or example.com/test, when we click on the "Back" button, we will return to the previous page (example.com/page or example.com/ test) because the previous page is within our example.com domain. And if we came to the example.com/page/1 page through Google, Yandex, etc., or from the browser start page, then when we click on the "Back" button, we will be returned not to Google, for example, but to the main page of the example.com site .
Button
onclick="goBack()">Back
Javascript
function goBack() {
window.history.back();
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2016-10-12
@xmoonlight

Reopen the site in a new tab.
https://developer.mozilla.org/en/docs/Web/API/History

O
Oleg, 2016-10-13
@politon

function goBack() {
   var a = document.referrer;
   var b= new URL(a).hostname;
  var c = location.host;
    if (b == c) {
       window.history.back();
    }else{
        alert("что то пошло не так");//else можешь удалить
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question