2012-03-26 15 views

答えて

1

あなたはonbeforeunloadに結合し、文字列を返すようにしようとしているので、私はあなたがをユーザに提供するために探していることを、推測していますAJAXサイトに「このページを残してもよろしいですか?」というダイアログが表示されますか?あなたはおそらく、リンク上にクリックハンドラを結合することによって少し異なり、このことについて移動する必要があり、その場合には

。したがって、確認が行われるまでハッシュ変更を防ぐことができます。以下のような

何か:

$('a[href^="#"]').live('click',function(e){ 
    if(//should we be confirming first?) { 
     //put your confirmation code here either using default JS windows or your own CSS/jQueryUI dialog boxes 
     // this code should either cache the url of the link that was clicked and manually update the location with it when the user confirms the dialog box (if you're using JQUI windows) or simply use JS confirmation boxes and based on the response, all you need to do is return; and the link click will handle normally 
     e.preventDefault(); //prevent the link from changing the hash tag just yet 
     e.stopImmediatePropagation(); //prevent any parent elements from firing any events for this click 
    } 
}); 
+0

ええ、それは動作します!ありがとう:) –

1

は誤解しないでください、しかし、あなたは深刻ですか?

このリンクはハッシュタグを参照するだけで、現在のサイトを離れず、onbeforeunloadunloadも呼び出されません。

インスタンスの任意の*クリックイベントハンドラbound to that anchor aswell, there must be something in the event handler code which really forces the current site to get unloaded ( location.href`がある場合)。

あなただけのAjax経由でHTMLを切り替える場合、onbeforeunload aswellはありません。

ハンドラをonhashchangeイベントにバインドできます(ブラウザの互換性を確認してください)が、あなたのURL /ハッシュで発生する変更に対して発生します。

+0

ええ、私はそれを知っています。しかし、ユーザーの観点から、彼はページを変更しました。ですから、質問はもっとです:私はこれを防ぐことができます、私はonbeforeunloadのトリックでそれを防ぐことができたでしょうか? –

+0

@MaximeARNSTAMM:確かにこの場合、あなたはajaxをやっている場所を見つけて、確認ロジックを入れる前に見つける必要がありますか? – Chris

関連する問題