Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. I am totally new to the history.pushstate and history.popstate methods which I am trying to implement on my new website. 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.

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. i.e. it seems that I have to click it twice for the popstate function to fire. 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. 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:

<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...

<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...

<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 (i.e. not working). 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...

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!

like image 661
Claud Avatar asked Oct 31 '25 05:10

Claud


1 Answers

OK, I worked it out... It was the iframe which was the issue, changing the src was pushing a history entry from the iframe. Thus the first click on back was taking the iframe back and not firing the top window popstate. My solution was to re-write the iframe. I've update my "question" above with my new code.

like image 108
Claud Avatar answered Nov 02 '25 20:11

Claud