TheBokiya
TheBokiya

Reputation: 620

Close Jquery dialog when the user presses browser's back button

I have this problem where the Jquery UI dialog does not close when the use presses the browser's back button. I've looked around for an answer and I found onhashchange event which I'm unable to use because the page that I have doesn't send an httprequest when the dialog is open so back button wouldn't send a new request either.

Can anyone help explaining to me why it doesn't send an httprequest when the dialog is opened? And how can I close the dialog on back button pressed?

Thanks

Below is my JavaScript code.

// Functions to open VM popups
    function openVMDialog(vmId) {
        fetch.Ajax(vmDetailUrl + '?vmId=' + vmId, beforeDialogSend, onDialogSuccess, onDialogError);
        // Not using pushState on browsers that don't support pushState (IE<10), pjax can fail gracefully without $.support.pjax check
        // but IE causes pjax to scroll to top. Check support.pjax here to prevent that from happening on IE.
        if ($.support.pjax) {
            @Model.AccountId.HasValue.ToString().ToLower()
            ? window.history.pushState(null, null, indexUrl + "&vmId=" + vmId )
            : window.history.pushState(null, null, "?vmId=" + vmId);
        }

    };
    function beforeDialogSend() {
        $("#popup").html('<div class="loader"><img src="@Links.Content.loader_gif" alt="loader" /></loader>').dialog('open');
    };
    function onDialogSuccess(data) {
        $("#popup").html(data).hide().fadeIn(1000).dialog({position: 'center'});
    };
    function onDialogError() {
        $("#popup").html('<p class="loader">Uh oh! A <em>Server Error</em> Occurred</p>');
    };

    function openDialogRestrictions(vmId) {
        // Disable background scroll when dialog is open
        if ($(document).height() > $(window).height()) {
            var scrollTop = ($('html').scrollTop()) ? $('html').scrollTop() : $('body').scrollTop();
            $('html').addClass('disableScroll').css('top',-scrollTop);
        }
        //Click anywhere to close
        jQuery('.ui-widget-overlay').bind('click', function() {
            jQuery('#popup').dialog('close');
        })
        setGifVisibility("live-gif", false);
    };

    function closeDialogRestrictions() {
        // Enable background scroll when dialog is close
        var scrollTop = parseInt($('html').css('top'));
        $('html').removeClass('disableScroll');
        $('html,body').scrollTop(-scrollTop);
        if ($.support.pjax) window.history.pushState(null, null, indexUrl);
        setGifVisibility("live-gif", true);
    };

    $(document).ready(function() {

        // Prevent pjax from scrolling to top.
        if ($.support.pjax) $.pjax.defaults.scrollTo = false;

        // Dialog settings for VMs
        $("#popup").dialog({
            autoOpen: false,
            position: 'center',
            width: 450,
            maxHeight: 600,
            minHeight: 450,
            resizable: false,
            draggable: false,
            closeOnEscape: true,
            modal: true,
            closeText: null,
            show: { effect: "clip", duration: 300 },
            hide: { effect: "clip", duration: 300 },
            dialogClass: 'fixed-dialog',
            open: openDialogRestrictions,
            close: closeDialogRestrictions
        });

        setHeight();
        loadContent();

        if (@Model.VMId.HasValue.ToString().ToLower()) openVMDialog(@Model.VMId);

        // Refreshes the page every 1 minute
        setInterval(loadContent, 60000);
    });

Upvotes: 0

Views: 3678

Answers (2)

pro
pro

Reputation: 97

try this

assuming on click of popup-btn, popup-panel will be shown.

Actions to be taken

  1. Click on Open Popup Panel link.
    The red popup-panel will be shown
  2. Click on browser back button.
    The red popup-panel will be gone

<style>
    .popup-panel
    {
        display:none;
        width:100px;
        height:100px;
        background:red;
    }
</style>

<a class="popup-btn" style="cursor:pointer;color:blue">Open Popup Panel</a>

<div class="popup-panel"></div>

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script> 
<script>
  
  $(document).ready(function(){
    
      $(".popup-btn").click(function () {
          location.hash = "popup";
          $(".popup-panel").show();
      });
    
  });
  
  $(window).bind('hashchange', function () {
    
      if (location.hash == null || location.hash == "") {
          $(".popup-panel").hide();
      }
    
  });
  
</script>

Upvotes: 3

malkam
malkam

Reputation: 2355

Try this

<script language="javascript">
$(document).ready(function () {
        $( window ).unload(function() {
          //alert( "Bye now!" );
            jQuery('#popup').dialog('close');
         });
});
</script>

Upvotes: 0

Related Questions