Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 521
Horatio Alderaan Avatar asked Feb 24 '12 00:02

Horatio Alderaan


People also ask

Can I use history back ()?

back() is the same as history.go(-1) . history. back() is the same as clicking "Back" your browser.

What is the syntax and use of history back () method?

The History back() method in HTML is used to load the previous URL in the history list. It has the same practical application as the back button in our web browsers. This method will not work if the previous page does not exist. This method does not contain any parameter.

How do you check if the user can go back in browser history or not?

To check if the user can go back in browser history or not with JavaScript, we can call history. back or history.go . history.go(-1); to go back to the previous page.

How does the Back button work in browser?

A back button in the browser lets you back-up to the copies of pages you visited previously. The web browser's back and next buttons work well with web sites that provide information that changes infrequently, such as news and shopping web sites.


1 Answers

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;
}
like image 103
Chibueze Opata Avatar answered Oct 04 '22 14:10

Chibueze Opata