0

戻るボタンが ajax 呼び出しで機能するように、 Asual Addressプラグインを使用しようとしています。私はこれがどのように機能するかについて非常に混乱しており、私が見つけたすべてのチュートリアルはそれを異なるものにするように言っています. 私はそれらのどれも機能させることができませんでした。

シンプルな ajax ページがあります。単純に画像を呼び出します。

Javascript:

<script type="text/javascript">    


$("a").live("click", function(event) {      
    url = $(this).attr("href");
     $.address.value($(this).attr('href')); 
    event.preventDefault();
        $.ajax({
            type: "get",
            url: "/"+url+".php",
            data: "",
            dataType: "html",
            success: function(html){
                jQuery('#Right_Content').hide().html(html).fadeIn(1000);
            },
         })

});
</script>

私のHtmlは次のようになります:

<body>
<a href="push1" onclick="return false;" >Image 1</a>
<a href="push2" onclick="return false;">Image 2</a>  

<div id="Right_Content"></div>

</body>

上記のコードを使用して、リンクをクリックすると ajax 呼び出しが行われ、写真が読み込まれ、URL が変更されます。これはうまくいっています。私の問題は戻るボタンです。戻るボタンをクリックすると、URL は前の URL に変わりますが、ページのコンテンツは同じままです。「popstate」とこれをjsに追加することに関係があると思います。方法がわかりません。

popstate がどのように機能するか、また、戻るボタンが機能するように上記のコードに追加する方法を誰かに説明してもらえますか。ありがとうございました。

全ページ

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script src="/JS_Functions/jquery-ui-1.8.17.custom/js/jquery-1.7.1.min.js"></script>
<script src="/JS_Functions/jquery-ui-1.8.17.custom/js/jquery-ui-1.8.17.custom.min.js"></script>
<script src="JS_Functions/jquery.address-1.4/jquery.address-1.4.min.js"></script>
<script type="text/javascript">
$.address.init(function(event) {
  // All elements with the "history" class will be "addressed". When they get clicked
  // the href will get used as the new #part of the URL
$('a').address();
$('a').live("click", function(){
    var href = $(this).attr( "href" );  
    $.ajax({
                        type: "get",
                        url: "/"+href+".php",
                        data: "",
                        dataType: "html",
                        success: function(html){
                        jQuery('#Right_Content').hide().html(html).fadeIn(1000);
                        },               
    })

})
}).change(function(event) {
  //Every time the url part changes this will be called. 

  // As an example here I determine the # part of the URL and default to 'home'
  var hash = event.path.substring(1);
  hash = hash? hash.toLowerCase(): 'push1';

  //.. do the stuff here that you need to do when hash has a certain value

});

</script>
</head>
<body>
<a href="push1" >Image 1</a>
<a href="push2" >Image 2</a>  

<div id="Right_Content"></div>



</body>
</html>
4

2 に答える 2

1

私の仕事は、ユーザーがいくつかのページに戻ることを許可し、他のページからデータを失うというメッセージなしで戻ることを防ぐことでした. 私はこの方法で実装を終了しました:

$(".-step-1-").click(function () {
   $.address.value("");
});

$(".-step-2-").click(function () {
    $.address.value("finances");
});

$(".-step-3-").click(function () {
    $.address.value("cofirmation");
});

global.previousPage = null;
$.address.init(function () {
}).change(function () {
}).externalChange(function (event) {
    if (global.previousPage == null) {
        $.address.value("");
    } else {
        showPageExternal(event.pathNames);
    }
}).internalChange(function (event) {
    global.previousPage = event.pathNames;
    showPage(event.pathNames);
});

var showPageExternal = function (pathName) {
    if (global.previousPage == null ||
        ("").localeCompare(global.previousPage) === 0 ||
        ("finances").localeCompare(global.previousPage) === 0) {
        showPage(pathName);
    }
    else {
        //This mean is coming from "confirmation" page
        global.blockUI($("#browserBackMessage"));
    }
};

var showPage = function (pathName) {
    if (("").localeCompare(pathName) === 0) {
        $(".step").hide();
        $(".-step-1").show();
    }
    if (("finances").localeCompare(pathName) === 0) {
        $(".step").hide();
        $("#finances").show();
    }
    if (("confirmation").localeCompare(pathName) === 0) {
        $(".step").hide();
        $("#confirm").show();
    }
};

global = function globalVariable() {
    return (function () { return this; })();
};
于 2013-05-02T22:23:34.000 に答える
0

これは私がhttp://www.isaac.nl/en/で使用したものです

私はそこにこのHTMLを持っています:

<div id="main-menu">    
  <a class="history" href="#home">Home</a>
  <a class="history" href="#who">who</a>
  <a class="history" href="#what"></a>
  <a class="history" href="#where"></a>  
</div>

js ファイルでは、次のようにします。

$.address.init(function(event) {
  // All elements with the "history" class will be "addressed". When they get clicked
  // the href will get used as the new #part of the URL
  $('.history').address();

}).change(function(event) {
  //Every time the url part changes this will be called. 

  // As an example here I determine the # part of the URL and default to 'home'
  var hash = event.path.substring(1);
  hash = hash? hash.toLowerCase(): 'home';

  //.. do the stuff here that you need to do when hash has a certain value
});
于 2012-03-07T21:58:16.293 に答える