简体   繁体   English

jQuery ajax中的哈希和pushstate

[英]hashing and pushstate in jquery ajax

I have 3 buttons 我有3个按钮

 <div onclick="javascript:newmessage()" class="mailcontents">New Message</div>
<div onclick="javascript:inboxmails()" class="mailcontents">Inbox</div>
<div onclick="javascript:sentmailsbox()" class="mailcontents">Sent</div>

and the divs where the content has to be displayed 以及必须显示内容的div

 <div id="inbox"></div>
<div id="sent"></div>

Onclick of each I am changing the content by calling the respective functions 每个人的onclick我通过调用相应的函数来更改内容

    var times = 0;
   function inboxmails(){

       times++;
location.hash = times;
       $('#inbox').empty();
       $('#sent').empty();
       $('#newmessage').empty();
       $.ajax({
                success: function (data) {
                    $('#inbox').append('inbox data');

                }
            });
        }
   function sentmailsbox(){
       times++;
location.hash = times;
       $('#inbox').empty();
       $('#sent').empty();
       $('#newmessage').empty();
       $.ajax({
                success: function (data) {
                    $('#sent').append('sent data');

                }
            });

   }
   function newmessage(){
       $('#inbox').empty();
       $('#sent').empty();
       $('#newmessage').empty();
       $.ajax({
                success: function (data) {
                    $('#newmessage').append('new message data');

                }
            });

   }

I have also added hashing which adds hash tag and a numberical value to the url 我还添加了哈希,该哈希向网址添加了哈希标签和数字值

Here is the plunker code https://plnkr.co/edit/tTMzTFIHD03pkIt4CkHO?p=preview 这是插件代码https://plnkr.co/edit/tTMzTFIHD03pkIt4CkHO?p=preview

But when I click on the back button of browser it must reload the previous ajax content. 但是,当我单击浏览器的后退按钮时,它必须重新加载以前的ajax内容。 ie, if I click on inbox , it displays inbox data and next I clicked on sent , it displays sent data , and then when I click the browser's back button it must display inbox content inbox data ie, the previous content. 即,如果我单击inbox ,它将显示inbox data ,然后单击“ sent ,它将显示已sent data ,然后,当我单击浏览器的“后退”按钮时,它必须显示收件箱内容,即收件inbox data即先前的内容。

I understood how to change a url using hashing. 我了解了如何使用哈希更改网址。 But I am unable to understand and also could not find a proper example to pushstate ie push history back / display the pervious ajax content. 但是我无法理解,也找不到合适的示例来推送状态,即往回推送历史/显示以前的ajax内容。

I have checked various examples. 我检查了各种例子。 But I am not understanding how to go to load ajax content using hashing. 但是我不明白如何使用散列来加载ajax内容。

Please help !!! 请帮忙 !!! Thanks in advance !!! 提前致谢 !!!

You've not bound any function to a hashtag change event. 您尚未将任何功能绑定到主题标签更改事件。 In JavaScript you could use this event to fire off the AJAX call again: 在JavaScript中,您可以使用此事件再次触发AJAX调用:

window.onhashchange = doThisWhenTheHashChanges;

See Mozilla 参见Mozilla

There is also this JQuery library you can look at which addresses cross browser compatibility issues and a full list of alternative solutions 您还可以通过此JQuery 查看哪些地址解决了跨浏览器兼容性问题以及替代解决方案的完整列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM