0

我正在使用 jQMobile 的单页 AJAX 加载。每个页面都是它自己的文件。

当我需要时,我会在我的页面上动态创建一个 HOME 按钮。今天我只使用了一个指向“index.html”的<A>标签。有什么方法可以检查我的网络应用程序 jQueryMobile 历史记录,以查找历史上第一次加载我的 index.html 页面并调用 window.history.back(-##); 到页面,而不是仅仅添加到历史和导航。

代码将是这样的,如果历史记录中没有 index.html,我将只是 window.location.href 到页面。

function
GoHome()
{

  /* This is a function I don't know even exists, but it would find the first occurrence of index.html */
  var togo = $mobile.urlHistory.find( 'index.html' )  
  var toback = $mobile.urlHistory.length -1 - togo;

  if ( togo >= 0 )
    window.history.back( -1 * toback )
  else
    $.mobile.changePage( '/index.html' )

}

如果历史是 index.html => profile.html => photos.html $.mobile.urlHistory.find('index.html') 的魔法函数会返回 0,历史长度是 3,所以我的计算是到 window.history.back( -2 ) 返回 index.html 页面。如果该 find 函数返回 -1 则未找到,我将只进行 changepage 调用。

谢谢/安迪

4

1 回答 1

0

在阅读和阅读和阅读之后,我不确定我在阅读什么,我写了这个函数。不确定它的正确性或最佳解决方案,或者即使它可以被浓缩。

用这个来调用

console.log( 'FindHome: ' + FindHome( ["", 'index.html'] ) );

它会从 jQueryMobile urlHistory.stack 中的第一个条目搜索到当前索引,并查看它是否会找到索引页面。从那里我可以决定如果我想加载一个新页面或返回 -xx 到历史上已经存在的页面该怎么做。这样历史就会有点干净。

function
FindHome( _home )
{
var stk = $.mobile.urlHistory.stack;
var ai  = $.mobile.urlHistory.activeIndex;

for ( var idx=0; idx <= ai; idx++ )
{
    var obj = $.mobile.path.parseUrl( $.mobile.urlHistory.stack[idx].url );
    if(typeof _home == "string")
    {
        if ( _home == obj.filename )
            return( idx - ai );
    }
    else
    {
        for(var x in _home)
        {
            if ( _home[x] == obj.filename )
                return( idx - ai );
        }               
    }
}

return ( 0 );   
}
于 2013-08-01T22:25:31.527 回答