The easiest way is to use history.go(-1);
Try this:
<a href="#" onclick="history.go(-1)">Go Back</a>
history.go(-1)
doesn't work if you click around in the 2nd domain or if the referrer is empty.
So we have to store the historyCount on arriving to this domain and go back the number of navigations in this side minus 1.
// if referrer is different from this site
if (!document.referrer.includes(window.location.host)) {
// store current history length
localStorage.setItem('historyLength', `${history.length}`);
}
// Return to stored referrer on logo click
document.querySelector('header .logo').addEventListener('click',
() =>
history.go(Number(localStorage.getItem('historyLength')) - history.length -1)
);
you can try javascript
<A HREF="javascript:history.go(-1)">
refer JavaScript Back Button
EDIT
to display url of refer http://www.javascriptkit.com/javatutors/crossmenu2.shtml
and send the element a itself in onmouseover as follow
function showtext(thetext) {
if (!document.getElementById)
return
textcontainerobj = document.getElementById("tabledescription")
browserdetect = textcontainerobj.filters ? "ie" : typeof textcontainerobj.style.MozOpacity == "string" ? "mozilla" : ""
instantset(baseopacity)
document.getElementById("tabledescription").innerHTML = thetext.href
highlighting = setInterval("gradualfade(textcontainerobj)", 50)
}
<a href="http://www.javascriptkit.com" onMouseover="showtext(this)" onMouseout="hidetext()">JavaScript Kit</a>
check jsfiddle
<a href="#" onclick="history.back();">Back</a>
For going back to previous page using Anchor Tag <a>
, below are 2 working methods and out of them 1st one is faster and have one great advantage in going back to previous page.
I have tried both methods.
1)
<a href="#" onclick="location.href = document.referrer; return false;">Go Back</a>
Above method (1) works great if you have clicked on a link and opened link in a New Tab in current browser window.
2)
<a href="javascript:history.back()">Go Back</a>
Above method (2) only works ok if you have clicked on a link and opened link in a Current Tab in current browser window.
It will not work if you have open link in New Tab. Here history.back()
will not work if link is opened in New Tab of web browser.
try this
<a href="javascript:history.go(-1)"> Go Back </a>