简体   繁体   English

jQuery AJAX后退按钮问题

[英]jquery AJAX backbutton problems

This is a bit weird to explain so ill do my best. 这有点怪异的解释,使我无法做到最好。

Im working on a mobile site that im trying to shape into an ios app (eventually). 我正在一个移动网站上工作,我试图将其最终转变为一个ios应用程序。 On this page i have a menu button that on click, shows/hides menu. 在此页面上,我有一个菜单按钮,单击该按钮可显示/隐藏菜单。

Everything for the most part is working but, the problem is that when you click on the "back" button and the browser runs out of previous locations to go back to, the menu button breaks. 大多数情况下一切正常,但是问题是,当您单击“后退”按钮并且浏览器用完了以前的位置无法返回时,菜单按钮会中断。 When you click it, nothing happens. 当您单击它时,什么也不会发生。 it behaves as though nothing is there. 它的行为好像什么都没有。

The errors go like this, if on index(or first page), you click ONE link to go forward, and then on the browser hit the back button, it breaks instantly. 错误是这样的,如果在索引(或第一页)上,单击“一个”链接前进,然后在浏览器中单击“后退”按钮,则它会立即中断。

If your on the index, and hit for example, 5 links in any order(essentially you went forward 5xs) and thereafter, you hit back up to 4xs, the menu still works....you can essentially keep pressing links forward and be fine, hit back as far as you want and stop short one less than the total times you went forward and still be fine. 如果您在索引上并按任意顺序命中了5个链接(本质上您前进了5xs),然后又将其向上推回了4xs,则菜单仍然有效。可以,只要回击就可以,并比前进的总次数少停一秒,但仍然可以。

As soon as you hit the maximum backs, and theres nothing left in the history of page locations to go back to, the menu then breaks. 一旦您获得最大支持,页面位置历史记录中就没有任何内容可返回,菜单就会中断。

here is the link of this dummy site im working on (on my server) NOTE: to test, when it loads, manually shrink the browser to about 400 width so you can see the page take effect. 这是我在(我的服务器上)正在工作的虚拟站点的链接注: 要进行测试,请在加载时手动将浏览器缩小到400左右的宽度,以便您看到页面生效。

EDIT** woops forgot the link http://somdowprod.net/4testing/mobile/less1.html 编辑**忘了链接http://somdowprod.net/4testing/mobile/less1.html

and here is my code 这是我的代码

javascript:(i left comments in there, so you can see where my logic is at...maybe im going about it wrong?) javascript :(我在那儿留言,所以您可以看到我的逻辑在哪里...也许我正在做错事吗?)

// JavaScript Document
$(document).ready(function(){
var newHash = "";
var menuBtn = $('.leftButton');

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~menu show/hide
    menuBtn.click( menuShowHide);
    //===========================//

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~MENU SHOW / HIDE
    function menuShowHide(){
        $('#menu').toggleClass();

//      if($('#menu').css("display") == "none"){
//          $('#menu').css("display","block");
//      }   else {
//          $('#menu').css("display","none");   
//      }
        scroll(0,0);    
    }
    //===========================//


    //~~~~~~~~~~~~~~~~~~~~~~~~~~~ACTIONS FOR THE is-loaded trick.
        function isLoaded(){
            $('#progress').remove();    
        }
    //===========================//



    //~~~~~~~~~~~~~~~~~~~~~~~~~~~ACTIONS FOR THE HOME PAGE
        if(newHash == ""){
            $('body').append('<div id="progress">Loading...</div>');//attach this div which is made to look like a loading bubble.
            $('#contentHere').load('index.html #content', isLoaded);//load the content div from the index.html file
        }
    //===========================//




    //~~~~~~~~~~~~~~~~~~~~~~~~~~~Load the clicked content into my container via jQuery AJAX
    $('#menu a').click(function(){
        menuShowHide();
        window.location.hash = $(this).attr('href');
        return false; //doesnt let the link jump to a new page

    });

    $(window).bind('hashchange',function(){
        newHash = window.location.hash.substring(1);
        $('body').append('<div id="progress">Loading...</div>');//attach this div which is made to look like a loading bubble.
        $('#contentHere').load(newHash, isLoaded);
        //console.log(newHash); 

    });

    //===========================//



});

Anyone have any ideas as to whats making the menu break? 有人对菜单中断有什么想法吗? Thanks in advanced. 提前致谢。

The hashchange event is known to be unpredictable with the browsers back button. 通过浏览器的后退按钮,hashchange事件是不可预测的。

Here's what's happening. 这是正在发生的事情。 When you back all the way to the beginning, the whole less.html page is being loaded into its own 'contentHere' container, and since document.ready won't be called on this subpage, no menuButton.click handler will be assigned to the links, so clicking them will not do anything. 从头开始时,整个less.html页面将被加载到其自己的'contentHere'容器中,并且由于不会在此子页面上调用document.ready,因此不会将menuButton.click处理程序分配给链接,因此单击它们不会执行任何操作。

Although being able to use your browsers back and forward buttons on asynchronous changes would be a neat feature, I don't think people would miss it enough for you to try to hack a workaround. 尽管能够在异步更改上使用浏览器的前进和后退按钮将是一个很好的功能,但是我认为人们不会错过足够多的时间来尝试破解解决方法。 People are used to not being able to use the back and forward button on asynchronously loaded content. 人们习惯于不能在异步加载的内容上使用后退和前进按钮。 They would try it once, see that it takes them to your site's referrer, hit forward, and subconciously make a mental note to use your menu instead of the back and forward buttons. 他们会尝试一次,看到它会带他们到您网站的引荐来源网址,向前进行搜索,然后下意识地记下要使用菜单而不是后退和前进按钮的信息。

Probably a better suggestion, however, is to get rid of the AJAX feature altogether and let the links act the way that people expect them to. 但是,可能更好的建议是完全放弃AJAX功能,让链接按照人们期望的方式运行。 Okay, so you lose the cool 'Loading' modal and you save the user a tiny bit of time, but then you aren't confusing people's expectations about your interface. 好的,这样您就失去了炫酷的“加载”模式,并为用户节省了一点时间,但是您并不会混淆人们对界面的期望。

Edit: to answer your question below, instead of using load() to get static content (which is overkill), put all of the content as sections in your less.html file and hide/unhide it using the menu selections. 编辑:在下面回答您的问题,而不是使用load()获得静态内容(这是过大的选择),而是将所有内容作为小节放置在less.html文件中,并使用菜单选择隐藏/取消隐藏它。 Here are two ways to accomplish this, each with their own advantages and disadvantages: 这是完成此操作的两种方法,每种都有其优点和缺点:

  1. Fix your header with CSS (including the menu) (example: lifeinthegrid.com/simple-css-fixed-header/) and then make your menu links normal anchor links with no JavaScript event handler. 使用CSS(包括菜单)修复标题(例如:lifeinthegrid.com/simple-css-fixed-header/),然后使菜单链接成为不带JavaScript事件处理程序的普通锚链接。 When you click a link in the menu, the content jumps to that section and because your header is fixed, it has the feel of being a very fast httpRequest. 当您单击菜单中的链接时,内容跳至该部分,并且由于标题是固定的,因此感觉就像是一个非常快速的httpRequest。 Advantages: minimal JavaScript, the browser's back and forward buttons will work. 优点:最少的JavaScript,浏览器的后退和前进按钮将起作用。 Disadvantages: fixed elements are a nuisance on older smartphones, the next section might sneak into the bottom of the viewport and ruin the illusion. 缺点:固定元素在较旧的智能手机上很麻烦,下一节可能会潜入视口底部并破坏幻觉。 Solution: put more space between them. 解决方案:在它们之间放置更多空间。

  2. The other method is to use show()/hide() on the different content sections. 另一种方法是在不同的内容部分上使用show()/ hide()。 So let's say you click 'about us'. 假设您点击“关于我们”。 All of the sections are hidden and the 'about us' content is shown. 隐藏所有部分,并显示“关于我们”的内容。 Advantages: It's not as annoying as a fixed element, you can spice it up with FadeIn()/FadeOut() or other JQuery animation effects. 优点:它不像固定元素那样令人讨厌,您可以使用FadeIn()/ FadeOut()或其他JQuery动画效果为其增添趣味。 Disadvantages: your browsers history will not track these changes unless you do some window.location hackery. 缺点:除非您进行一些window.location黑客操作,否则您的浏览器历史记录将不会跟踪这些更改。

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

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