16

I'm building an application in an environment where I'm restricted to using the local file system and a browser (i.e. running a server isn't an option). I have a generic 'go back' link on numerous pages that mainly just calls history.back(). It looks something like the following:

function goBack(evt) {
    // Check to see if override is needed here

    // If no override needed, call history.back()
    history.back();
}

$('#my-back-button').click(goBack);

This code works fine in Firefox and IE6 (don't ask), but fails in Chrome. Any suggestions as to why and/or possible workarounds?

I've also tried history.go(-1); which does not work either.

Horatio Alderaan
  • 3,264
  • 2
  • 24
  • 30

5 Answers5

25

For some reason in chrome, you have to add return false after calling history.go(-1)

Change your function to:

function goBack(evt) {
// Check to see if override is needed here

// If no override needed, call history.back()
history.go(-1);
return false;
}
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
    function goBack(evt) {
    // Check to see if override is needed here

    // If no override needed, call history.back()
    history.back();
    $('#my-back-button').forwardEvent('click');
}

$('#my-back-button').click(goBack);

/**
* chrome workaround for triggering click events
* @param {event} event  event
* @return {undefined}   Returns undefined
*/
    $.fn.forwardEvent = function(event) {
        this.each(function() {
            if (this.dispatchEvent) {
                if (event.originalEvent) {
                    event = event.originalEvent
                }
                try {
                    this.dispatchEvent(event);
                } catch(error) {
                    $(this).trigger(event);
                }
            }
            else {
                $(this).trigger(event);
            }
        });
        return this;
    };
});
</script>
<input type="button" value="<<<<" id="my-back-button">
</body>
</html>
heliogabal
  • 613
  • 2
  • 7
  • 16
  • Hmmm. I'm sorry but this just seems to create an infinite recursion. The issue isn't the $('#my-back-button') not receiving the click event, but rather that history.back() does nothing. – Horatio Alderaan Mar 02 '12 at 01:03
  • Well, I've tested it in chrome vs. Your original code, and it works. With "file:///" protocol etc. Didn't work for You? – heliogabal Mar 02 '12 at 07:24
  • Ok, epic fail here, because it works for me anyway, without event forwarding... Could You paste the exact code of page used for testing? Stripped from unncessary stuff of course, bare minimum. – heliogabal Mar 02 '12 at 08:39
1

var referrer = document.referrer; window.location.replace(referrer);

Use this it will work

0

For Chrome use below code:

<a href="#" onclick="javascript:history.go(-1);return false;" style="text-decoration:underline;">Back</a>

Only this code will work..

I hope it will help... Happy coding.. :)

Viishnuu
  • 69
  • 2
  • In general, using "onclick" (and similar) to set event handlers is considered bad practice. See: http://stackoverflow.com/questions/5871640/why-is-using-onclick-in-html-a-bad-practice, for more information. – Horatio Alderaan Oct 09 '13 at 03:56
-1

late to the party... here is another solution: you can close the window following the window.history.go(-1). this will work because chrome will keep the window open so it will read the next line. other browsers will simply go back.

<script>
alert("Success!");
window.history.go(-1);
window.close();
</script>
Darren
  • 11
  • 3
  • From the question: "I've also tried history.go(-1); which does not work either." – Stephan Vierkant Feb 12 '19 at 15:17
  • it works for all other browsers except chrome, so just add the close script after the window open call and it will close out that window and be back where you started if chrome opened a new tab which is usually the problem. – Darren Feb 12 '19 at 15:25
  • The question is about Chrome and your answer doesn't work according to the question itself. There is already an accepted answer and your answer doesn't add anything. – Stephan Vierkant Feb 12 '19 at 16:14
  • my answer is about chrome and it is a solution to the original problem. – Darren Feb 12 '19 at 16:37