How to go back to the previous page when you click esc?

2

I have an item list in which to click on any item, and go to another page for item details. I would like to click the ESC button on the keyboard, go back to the previous page.

Basically do the browser navigation function by going back to the previous page, like the image below:

How do I go back to the previous page when I click esc?

    
asked by anonymous 08.05.2017 / 16:52

2 answers

3

Simply check that the tight key was ESC ( key Code 27 ) and use Window.history for return to the previous page.

See the example below:

document.onkeyup = function(e) {
  e = e || window.event; //compatibilidade com navegadores antigos IE

  if (e.keyCode == 27) {
    history.back(); //ou history.go(-1); 
  }
};

Example in JSFiddle.

If you want to understand more about Browse History you can see more details here .

This question has some other examples of how to do what you want.

If you'd like to better understand how each event works, you can see this example that I took #

08.05.2017 / 17:00
1

For your case it may be a bit of a bummer, but you can use the library I've built KeyMap .

document.onKeyMap('esc', function(){
  // history.back(); <-- COGIGO REAL
  alert('ESC');
});
<script src="https://cdn.rawgit.com/Lautert/KeyMap/master/KeyMap.js"></script>
    
08.05.2017 / 17:08