简体   繁体   中英

A button to change 'javascript:history.go() ' history

Usually we use <a href="javascript:history.go(-1)">Back</a> to go back page.

I would like to build a button to increase -1 value.

For example, when I click a button history.go(-2) click again history.go(-3) like this.

Is it possible?

Here's an example function using closures :

<a id="backer" href="">back</a>
...
document.getElementById('backer').onclick = (function () {
  var x = -1;
  return function () {
      console.log(x--)
  }
})();

Obviously, replace console.log with history.go .


EDIT

I just thought about the fact that this is using the back button. You could use cookies for IE7 coverage, but localStorage is so much cleaner.

<a id="backer" href="">back</a>
...
document.getElementById('backer').onclick = (function () {
  var x = window.localStorage['background'] | 0;
  return function () {
      x = x - 1;       
      window.localStorage['background'] = x;  
      history.back(--x)
  }
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM