2012-04-01 16 views
4

window.locationの変更でwindow.history.goコマンドが正常に実行されたかどうかを確認する方法はありますか。window.history.goが正常に実行されているかどうかを確認しますか?

つまり、ヒストリスタックに3ページしかないときにwindow.history.go(-5)を実行すると、ブラウザは何も行いません。

他のコードが実行されているかどうかを確認する方法はありますか?ソートのエラーコールバック。

ありがとうございました。

答えて

3

すぐに応答するには、history.lengthを確認して、少なくとも6であることを確認する必要があります。行く-5。それとは別に、唯一の方法はsetTimeoutを使用し、スクリプトがまだ実行されている場合はコールバックが実行されることです。

+0

私は 'window.history.go( - (window.length-1))'を実際に実行していますので、有用な方法で 'history.length'をどのようにチェックするかわかりません。 'の場合(window.length> = history.length)'それはおそらく動作しないことができます:私は、私は 'setTimeout'方法を試してみて、それがこの目的のために動作するかどうかを確認... – leomancini

+0

@leomanciniと思います。 – Ryan

+0

Ack。私は間違った。私は 'window.history.go( - (window.history.length-1));;' – leomancini

0
本当に

ないJSの専門家が、あなたは、ユーザーがバックまたは前進したときに、いくつかのアクションを実行したい場合は、URLのハッシュを使用してjQueryのにonhashchangeイベントを使用して、いくつかの機能を引き起こす可能性があります。これはあなたに歴史上の地位を与えるものではなく、ブラウザ間の互換性についてもわかりませんが、これまでのところ私にとってはうまくいきました。

$(window).on('load' function(){ 
    var hash = parent.top.location.hash; 
    if(hash == '' || hash == '#' || hash == null){ 
     //if none, set a hash and reload page 
     parent.top.location.hash = '#/some/hash'; 
     parent.top.location.reload(true);//use true if you dont want to use cached items 
    } 
}); 
$(window).on('hashchange', function(){ 
    do_something(parent.top.location.hash); 
}); 
function do_something(hash){ 
    //this function will be executed each time the '#' changes 
    console.log('hash changed to '+hash); 
} 
関連する問題