2012-07-16 69 views
0

當從頁面1轉到頁面2時,嘗試在頁面上手動設置data-url屬性時,我產生了奇怪的效果。如何設置Jquery Mobile頁面的data-url屬性?

我想設置的頁面數據url屬性的URL路徑,像這樣:

page.attr({ 'data-url' : $.mobile.path.parseUrl(window.location.href).pathname }); 

現在我就pagebeforeshow這樣做。問題是,如果我不等待至少400毫秒,數據網址將始終設置爲上一頁的網址。所以我這樣做,我認爲這是非常糟糕......

window.setTimeout(function() { 
    page.attr({ 
     'data-url': $.mobile.path.parseUrl(window.location.href).pathname 
    }); 
}, 400) 

問題
難道這是因爲我聽pagebeforeshow與pageshow?我怎樣才能確保沒有超時,一個新的頁面被拉到DOM沒有得到先前訪問過的頁面的路徑名(這導致了一些令人困惑的導航...

回答

0

問題是pushstate隱藏實際發生的事情。

如果從www.site.com/folder/page1.htmlpage2.html在jQuery Mobile的去,你沒有pushState的網址是這樣的:

www.site.com/folder/page1.html/#/folder/page2.html 

這pushState的面具成

www.site.com/folder/page2.html 

但是如果你使用parseURl.pathname,路徑仍然會

www.site.com/folder/page1.html 

因此,數據的URL設置爲路徑將始終將其設置爲

www.site.com/folder/page1.html 

因爲是......路徑名。 Pushstate只是不顯示它。我現在正在這樣做,沒有超時就可以正常工作:

setPage = $.mobile.path.parseUrl(window.location.href); 
    setPageUrl = setPage.hash == "" ? setPage.pathname : setPage.hash.replace('#',''); 
    page.attr({ 'data-url' : setPageUrl });