【发布时间】:2015-03-05 17:42:35
【问题描述】:
假设我们有一个<p:commandLink action="…" onclick="history.pushState(…)">,它对页面状态进行了重要更改。 Primefaces 5.1 生成此 HTML:
<a id="link1" href="#"
onclick="history.pushState( {currentStateKeyWord: 'state1'},'','state1');
PrimeFacesGeneratedFunction(stuff);">Click me for state 1</a>
<a id="link2" href="#"
onclick="history.pushState( {currentStateKeyWord: 'state2'},'','state2');
PrimeFacesGeneratedFunction(stuff);">Click me for state 2</a>
在popstate 事件处理程序中,我们必须根据pushState 中作为第一个参数推送的对象来恢复状态。使用 JQuery:
jQuery(document).ready(function(jQuery) {
jQuery(window).bind('popstate', function(event) {
if ((event.originalEvent.state!==null)
&& (event.originalEvent.state.currentStateKeyWord!==undefined)) {
switch (event.originalEvent.state.currentStateKeyWord) {
case 'state1':
jQuery("#link1").click();
break;
case 'state2':
jQuery("#link2").click();
break;
default:
console.error("Unknown state");
}
}
}
}
为什么这不起作用:使用jQuery("#link1").click(); 会强制链接就像用户点击它一样工作,这很好,但不幸的是popstate 事件处理程序不能打电话给history.pushState(在onclick里面)。在 Firefox 中,此代码破坏了前进按钮,这是不可取的。
问题:正确编写对history.pushState 和popstate 事件处理程序的调用的最简单方法是什么,考虑到无论我们做什么,popstate 都必须触发打电话给PrimeFacesGeneratedFunction(stuff)?
谢谢!
【问题讨论】:
-
为什么不能直接在
popstate事件处理程序中调用PrimeFacesGeneratedFunction()而不是间接调用? -
因为不知道生成的函数叫什么名字,也不知道参数。
-
所以在你的
commandLink标签中,你用pushState指定onclick属性,但是当它被翻译成HTML时,除了你提到的之外,PrimeFaces添加了一些额外的js函数来调用onclick在 commandLink 中,我的理解正确吗? -
是的,你理解正确。我通过使用不同的 JSF 代码解决了这个问题。
标签: javascript jquery primefaces html5-history