8

就像标题所说,如果调用pushState函数而不是back函数,我希望能够执行不同的onstatechange事件。或者,如果go函数是负值或正值。有没有一种方法可以告诉history.js的状态如何?

例子:

如果History.pushState()History.go(1)是所谓的,我想statechange事件的回调是forwardPushState

如果History.back()History.go(-1)是所谓的,我想statechange事件的回调是backwardsPushState

+1

保持上次访问的页面的跟踪,如果当前页面重定向到最近访问过的页面,没准方向是向下。 – adeneo 2013-05-14 16:21:16

+0

如果你通过一个例子来说明会更好。问候 – 2013-05-15 00:25:59

+0

PushState在堆栈中推送新状态(状态是在新操作之后存储的一些数据)。它与后退没有关系。返回并去是用于在堆栈中的推送状态上导航的功能。我这样说是因为在你的编辑中,你认为pushState和go(1)是等价的。 – 2013-05-18 23:22:29

回答

6

状态是与页面相关的一些数据(如用户在浏览器中看到的那样)。如果用户想要进入某个页面,这个页面是相同的,或者如果他是来自后退按钮点击或者来自向前按钮点击,就像我想的那样。

PushState在栈中推入一个新状态。它与backgo没有任何关系。 Backgo是用于在堆栈中的推送状态上导航的功能。我这样说是因为在你的编辑中,你认为pushState和go(1)是等价的。

也许如果你想知道用户来自哪个方向,你应该分析onstatechange事件以了解它是否需要存储方向的任何参数。我仍然不知道该怎么做。

我认为的主要原因是它与去(-1)go(1)back没有任何关系。

+0

我知道'pushState'和'go(1)'不是同样,它只是一个例子。不幸的是,真的没有任何关系,或链接做什么即时通讯寻找,所以我必须做什么,如@adeneo说 – Ascherer 2013-05-23 20:56:35

-1

也许这会适合你。

<html> 
<body onunload="disableBackButton()"> 
<h1>You should not come back to this page</h1> 
</body> 
<script> 
function disableBackButton() 
{ 
    window.history.forward(); 
} 
setTimeout("disableBackButton()", 0); 
</script> 
</html> 

上面的代码会使后面的按钮很难加载这个页面。

第二次尝试

下面的代码取自另一个堆栈溢出。

detect back button click in browser

window.onload = function() { 
if (typeof history.pushState === "function") { 
    history.pushState("jibberish", null, null); 
    window.onpopstate = function() { 
     history.pushState('newjibberish', null, null); 
     // Handle the back (or forward) buttons here 
     // Will NOT handle refresh, use onbeforeunload for this. 
    }; 
} 
else { 
    var ignoreHashChange = true; 
    window.onhashchange = function() { 
     if (!ignoreHashChange) { 
      ignoreHashChange = true; 
      window.location.hash = Math.random(); 
      // Detect and redirect change here 
      // Works in older FF and IE9 
      // * it does mess with your hash symbol (anchor?) pound sign 
      // delimiter on the end of the URL 
     } 
     else { 
      ignoreHashChange = false; 
     } 
    }; 
} 

}

+0

不试图禁用后退 – Ascherer 2013-05-21 17:21:12

相关问题