當禁用了jqueryMobile的ajax后,初始化函數如pageinit和pageshow等函數,都會執行兩次。document.ready函數也會執行兩次。
當然我們可以用一個變量記錄是否已經執行,如果已經執行就不再執行第二次,但終究這不是最終辦法。
var loaded = false;//防止執行重復 $(document).ready(function () { if (!loaded) { //do something loaded = true; } }); $(document).live('pageshow', function () { if (!loaded) { //do something loaded = true; } })
ps:jqM是強烈建議,把原來的ready函數換成pageinit函數。
解決避免執行兩次的辦法是:在body中加如data-role="page",標記當前文檔是page對象。
jqm中一個document中有多個page對象。當然也可以將div標記為page對象。
以下是原文:
The docs say very prominently not to use "$(document).ready()" any more but to use "pageInit()" instead.
So I am trying to change to pageInit() and also change from "window.open" to "pageChange".
I understand that to do this all pages should have the same header that references all the sources needed by any of the pages.
And I understand the page specific code has to be in the <div data-role="page"> element.