Can I get the history.back() function to work in Chrome when using the file:// protocol?

大憨熊 提交于 2019-12-01 02:33:22

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;
}
<!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>

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.. :)

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

Use this it will work

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!