I'm trying to use a script to go back in history when coming from within the domain. Coming from another domain as for example google, they have to be redirect to the homepage. This is what I have at this moment:
$( document ).ready(function() {
$('.back-btn').on('click', function(event) {
var re = /^https?:\/\/example.com\//;
event.preventDefault();
if (re.test(document.referrer)) {
history.go(-1); // Only go back if the referrer is from your domain
} else {
window.location="example.com";
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="javascript:void(0)" class="back-btn">Click here for one level up</a>
Does someone see why it doesn't work?
Regards,
Erwin