jQuery + Ajax Hash / History and more

后端 未结 2 1907
抹茶落季
抹茶落季 2021-02-06 15:04

I am trying to get a handle on using URL hashes in jQuery to control history in Ajax and make links / pages book-markable. I have tried almost every plug-in out there, and I can

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 15:22

    Based off Tatu's answer, I just changed a few things to make this work for me. It keeps a history, so back and forward buttons work good. Here's what I have,

    $(function() {
    var current_hash = false;
    setInterval(function() {
        if(window.location.hash != current_hash) {
            current_hash = window.location.hash;
                if(current_hash=='#home'){
                    var month = '9';
                    var year ='2011';       
                    $.ajax({
                        type: "POST",
                        url: "/home.php",
                        data: "data+here",
                        success: function(msg){
                            $('#div').html(msg);
                        }       
                    });                     
                }
                else if(current_hash=='#edit'){
                    $.ajax({
                        type: "POST",
                        url: "/edit.php",
                        data: "data+here",
                        success: function(msg){
                            $('#div').html(msg);
                        }       
                    });             
                }
        }        
    }, 100);
    

    Then simply assign some hash's to the links href attribute;

                        
  • Home Page
  • Edit Page
  • It's not a whole rewrite, just basically added some if statements to the same thing, but it may help someone out.

提交回复
热议问题