简体   繁体   English

仅在2次单击后每两次触发一次窗口popstate

[英]window popstate only being triggered after 2 back clicks, and every two

OK, hi all, I am new to Stack Overflow (as far as being registered is concerned) but use it regularly to find solutions. 好的,大家好,我是Stack Overflow的新手(就注册而言),但是请经常使用它来查找解决方案。 I have had a search for my problem, and while I have found several articles and questions almost relating to my issue, none really answer my exact problem. 我一直在寻找我的问题,尽管我发现了几篇与我的问题几乎相关的文章和问题,但没有一个能真正回答我的确切问题。 So apologies if this has been asked a million times! 非常抱歉,如果有人问了一百万遍!

I am fairly new to JQuery, but have have some experience of JavaScript generally. 我对JQuery相当陌生,但是一般都有一些JavaScript经验。 I am totally new to the history.pushstate and history.popstate methods which I am trying to implement on my new website. 我对在新网站上尝试实现的history.pushstate和history.popstate方法完全陌生。 I am not using ajax to load content I am loading it in an iframe, but the same principles apply, I need the page URL to update accordingly and be able to go back and forwards in the browser. 我没有使用ajax加载内容,而是将其加载到iframe中,但是应用了相同的原理,我需要页面URL进行相应的更新,并且能够在浏览器中来回移动。

Please be gentle with me in your answers, while I am fairly competent, as you will see from my code I am not overly advanced! 在我相当称职的同时,请在回答时对我温柔一些,正如您从我的代码中看到的那样,我并不过分!

OK, I have successfully implemented the pushstate, and the page loading seems to work fine (the example is here... http://beta.casafondaradio.com ) but when I click the browser back button, it is not having the desired effect. 好的,我已经成功实现了pushstate,并且页面加载似乎可以正常工作(示例在这里…… http: //beta.casafondaradio.com),但是当我单击浏览器后退按钮时,它并没有达到预期的效果影响。 ie it seems that I have to click it twice for the popstate function to fire. 即,似乎我必须单击两次以触发popstate函数。 I can't work out what it is I am doing wrong. 我无法弄清楚我做错了什么。

The main function is fired with an onclick attribute on the relevant links. 在相关链接上使用onclick属性触发主要功能。 I know there is a more elegant way of doing this, but I just wanted to keep it simple and legible for me! 我知道这样做有更优雅的方法,但我只是想让它简单易懂!

My links (in ASP.NET) are formatted as follows: 我的链接(在ASP.NET中)的格式如下:

<li runat="server" data="home" id="liHome" class="menuList"><asp:HyperLink ID="hlHome" onclick="return pageLoader(this);" data="home" CssClass="menuLink" runat="server" ToolTip="Home | CasafondaRadio.com" NavigateUrl="~/"><i class="fa fa-home" aria-hidden="true"></i>&nbsp;HOME</asp:HyperLink></li>
<li runat="server" data="schedule" id="liSched" class="menuList"><asp:HyperLink ID="hlSched" onclick="return pageLoader(this);" data="schedule" CssClass="menuLink" runat="server" ToolTip="Schedule | CasafondaRadio.com" NavigateUrl="~/schedule.aspx"><i class="fa fa-clock-o" aria-hidden="true"></i>&nbsp;SCHEDULE</asp:HyperLink></li>
<li runat="server" data="events" id="liEvent" class="menuList"><asp:HyperLink ID="hlEvent" onclick="return pageLoader(this);" data="events" CssClass="menuLink" runat="server" ToolTip="Events | CasafondaRadio.com" NavigateUrl="~/events.aspx"><i class="fa fa-star" aria-hidden="true"></i>&nbsp;EVENTS</asp:HyperLink></li>
<li runat="server" data="residentdjs" id="liDJs" class="menuList"><asp:HyperLink ID="hlDJs" onclick="return pageLoader(this);" data="residentdjs" CssClass="menuLink" runat="server" ToolTip="Resident DJs | CasafondaRadio.com" NavigateUrl="~/residentdjs.aspx"><i class="fa fa-headphones" aria-hidden="true"></i>&nbsp;DJS</asp:HyperLink></li>
<li runat="server" data="about" id="liAbout" class="menuList"><asp:HyperLink ID="hlAbout" onclick="return pageLoader(this);" data="about" CssClass="menuLink" runat="server" ToolTip="About | CasafondaRadio.com" NavigateUrl="~/about.aspx"><i class="fa fa-info-circle" aria-hidden="true"></i>&nbsp;ABOUT</asp:HyperLink></li>
<li runat="server" data="contact" id="liCont" class="menuList"><asp:HyperLink ID="hlCont" onclick="return pageLoader(this);" data="contact" CssClass="menuLink" runat="server" ToolTip="Contact | CasafondaRadio.com" NavigateUrl="~/contact.aspx"><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;CONTACT</asp:HyperLink></li>
<li runat="server" data="webplayers" id="liPlay" class="menuList"><asp:HyperLink ID="hlPlay" onclick="return pageLoader(this);" data="webplayers" CssClass="menuLink" runat="server" ToolTip="Web Players | CasafondaRadio.com" NavigateUrl="~/webplayers.aspx"><i class="fa fa-cog" aria-hidden="true"></i>&nbsp;PLAYERS</asp:HyperLink></li>

<iframe id="contentFrame" frameborder="0" style="height:100px;" scrolling="no" src='<asp:Literal ID="mainFrameSource" runat="server"></asp:Literal>'></iframe>

My Javascript code in the head section is... 我在开头的Javascript代码是...

<script type="text/javascript">
        function resetiFrame(obj) {
            $('#contentFrame').css("height", "100px");
        }
        function resizeiFrame(obj) {
            if ($('#contentFrame').attr('src') != '') {
                var dochgt = $(obj.contentWindow.document).height();
                $('#contentFrame').css("height", dochgt + "px");
            }
        }

        // Page Loader Functions
        var pageUrl = "", pageTitle = "", pageRef = "", pageContent = "",
        lastPageUrl = "", lastPageTitle = "", lastPageRef = "", lastPageContent = "";

        init = function() {
            // Do this when a page loads.
            // Page initialisation stuff will go here
        };

        function pageLoader(objLink) {
            var data = objLink.getAttribute('data'),
                titText = objLink.getAttribute('title'),
                url = objLink.getAttribute('href');

            // alert('data = ' + data + '\ntitle = ' + titText + '\nurl = ' + url);

            lastPageUrl = pageUrl; lastPageTitle = pageTitle; lastPageRef = pageRef; lastPageContent = pageContent;
            pageUrl = url; pageRef = data; pageTitle = titText; pageContent = data + "Content.aspx";
            historyUpdate(pageRef, pageTitle, pageUrl);
            loadPageData(pageContent);
            addActiveClass(data);
            return false;
        }

        function historyUpdate(data, title, url) {
            window.history.pushState(data, title, url);
        }

        function loadPageData(urlToOpen) {
            $("#contentFrame").attr('src', urlToOpen);
            init();
        }

        function addActiveClass(data) {
            $('#list li').removeClass('active');
            $('.menuList').filter('[data="' + data + '"]').addClass('active');
        }
    </script>

My javascript code in the before body close tag is... 我的主体关闭标签中的javascript代码是...

<script type="text/javascript">
        theiFrame = document.getElementById("contentFrame");

        $('#contentFrame').load(function () {
            resetiFrame(theiFrame);
            resizeiFrame(theiFrame);
        });

        $(window).resize(function () {
            resetiFrame(theiFrame);
            resizeiFrame(theiFrame);
        });

        $(window).on("popstate", function (e) {
            var origState = e.originalEvent.state;
            if (origState !== null) {
                // Load previous content
                alert(origState);
                if (origState == lastPageRef) {
                    alert("is last Page Ref!");
                }
            } else {
                alert('else : ' + e.originalEvent.state);
            }
        });

        init();
    </script>

As mentioned I have uploaded the example to the beta URL linked above and it is in the current state (ie not working). 如前所述,我已将示例上载到上面链接的beta URL,并且它处于当前状态(即不起作用)。 Any help would be greatly appreciated, clearly I am doing something wrong, but I have no clue what it is. 任何帮助将不胜感激,显然我做错了什么,但我不知道这是什么。

Thanks in advance! 提前致谢!

RESOLVED! 解决! Modified my loadPageData() function as follows... 修改了我的loadPageData()函数,如下所示...

function loadPageData(urlToOpen) { $('#contentFrame').remove(); var ifr = $('<iframe/>', { id: 'contentFrame', src: '/' + urlToOpen, style: 'height:100px;', load: function () { resizeiFrame(); } }); $('#frameContainer').append(ifr); }

And added an ID of frameContainer to the DIV with the original iframe in it! 并在其中包含原始iframe的DIV中添加了一个frameContainer的ID!

OK, I worked it out... It was the iframe which was the issue, changing the src was pushing a history entry from the iframe. 好的,我解决了...问题是iframe ,更改src会从iframe推送历史记录条目。 Thus the first click on back was taking the iframe back and not firing the top window popstate . 因此,第一次单击后popstate iframe退回,而不触发顶部window popstate My solution was to re-write the iframe . 我的解决方案是重新编写iframe I've update my "question" above with my new code. 我已经用新代码更新了上面的“问题”。

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

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