我使用JQuery動態追加GWT腳本,然後使用JQuery歷史跟蹤歷史記錄。JQuery和GWT歷史記錄支持混合起來
問題:我的GWT模塊生成History
標記,因爲我的所有GWT模塊都是MVP模塊。並且onClick()
s的MenuItem
s正在動態加載GWT模塊,以及我正在使用JQuery爲MenuItems
添加歷史記錄。但我的GWT模塊也使用History
。現在的問題是,如果我反覆點擊加載GWT MVP modules
的MenuItem
,那麼我的瀏覽器正在被攻擊。
代碼:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/newclickmenu/jquery.history.js"></script>
// Contains code which creates menuItems using JSTL. In this menuItem there will be menuItem and *subMenuItem(onclick of menuItem there will be a slideDown which would show subMenuItem
<script type="text/javascript">
jQuery(window).load(function() {
$('body').on('click', '#subMenuItem', function(event) {
// onClick of subMenuItem(which is anchor) then it add the History and will invoke the GWT mvp module.
// After doing the push then below `$.history.on('load change push', function(event, url, type) {` line is called.
$.history.push($(this).attr("href"));
event.preventDefault();
});
$.ajaxSetup({ cache: true });
$.history.on('load change push', function(event, url, type) {
if (event.type == "load") {
if (url.split("!").length < 2) {
window.location = url;
}
} else {
// This code will add the GWT module dynamically.
if (typeof url !== undefined && url !== null && url !== "") {
// Remove the old GWT script and append the new script
var previousModule = $(".mainModule");
if (previousModule.parent().length !== 0) {
$(previousModule).remove();
}
var expectedUrl = "<script class='mainModule' src='/Masters/Masters.cache.js'/>";
$('head').append($(expectedUrl));
}
}
)};
}).listen('hash');
</script>
我建議你應該添加你的一些代碼。 – qben
我已經添加了代碼,請檢查它。 –