Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run javascript after mobile 'back' press

We have a mobile site (ASP.NET in MVC style) which, among other things, displays a fullscreen HTML slideshow.

To display this, we run code on the mobileHTMLMovie view like so:

$( function() {
    $(document).bind('pageinit', function() {
    $("header").hide();
    $(".subHeader").hide();
    $(".subHeaderAccent").hide();
    $("footer").hide();
    $(".ui-content").css("width", "100%").css("height", "100%").css("margin","0").css("background-color", "#000");
    });
});

The problem we're seeing is that when the user pushes the 'back' button (s/he's limited to the hardware controls, as this code disables all the wrapper elements, including our HTML movement buttons) the elements remain hidden.

I have been unable to find a way to run the 'reversal' code (show instead of hide, removing the style attribute from ".ui-content") on the page they land on. The page they land on doesn't run document.ready when they arrive, the movie page doesn't run 'onunload' or 'onbeforeunload', and the other suggestion I've found online (.live('click') on the '[data-rel=back]' element) hasn't worked either.

Does anyone have any suggestions?

like image 307
Jeff Avatar asked Dec 26 '12 16:12

Jeff


1 Answers

Could you try to use the History object and bind to the popstate event? Basically, when the slideshow is displayed, you would push a new state in, and then when the user presses back, it might trigger the pop.

Example:

$(function() {
    $(document).bind('pageinit', function() {
        $("header").hide();
        $(".subHeader").hide();
        $(".subHeaderAccent").hide();
        $("footer").hide();
        $(".ui-content").css("width", "100%").css("height", "100%").css("margin","0").css("background-color", "#000");

        window.history.replaceState("slideshow", "Slideshow", "slideshow");
    });

    window.addEventListener('onpopstate', function(event) {
        if (event.state === "slideshow") {
            //Close the slideshow
        }
    }
});

I'm not sure what the flow is for the page, but you'd either use replaceState or pushState depending on how the slideshow is launched

Resources

  • https://developer.mozilla.org/en-US/docs/DOM/window.history
  • https://developer.mozilla.org/en-US/docs/DOM/Manipulating_the_browser_history
like image 194
Andrew Burgess Avatar answered Oct 03 '22 00:10

Andrew Burgess