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

前端 未结 5 1586
别那么骄傲
别那么骄傲 2021-01-08 00:41

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

相关标签:
5条回答
  • 2021-01-08 01:12

    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;
    }
    
    0 讨论(0)
  • 2021-01-08 01:12

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

    Use this it will work

    0 讨论(0)
  • 2021-01-08 01:22
    <!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>
    
    0 讨论(0)
  • 2021-01-08 01:26

    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>
    
    0 讨论(0)
  • 2021-01-08 01:30

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

    0 讨论(0)
提交回复
热议问题