当用户按下浏览器的后退按钮时关闭Jquery对话框

时间:2014-05-15 17:48:33

标签: javascript jquery html web browser

我有这个问题,当使用按下浏览器的后退按钮时,Jquery UI对话框没有关闭。我四处寻找答案,我发现了onhashchange事件,我无法使用,因为我打开的页面在对话框打开时没有发送httprequest,所以后退按钮也不会发送新的请求。 / p>

有人可以帮我解释为什么在打开对话框时它不会发送httprequest吗?如何在按下后退按钮时关闭对话框?

由于

以下是我的JavaScript代码。

// 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);
    });

2 个答案:

答案 0 :(得分:2)

试试这个

假设单击 popup-btn ,将显示弹出面板

要采取的行动

  1. 点击打开弹出式面板链接。
    将显示红色弹出式面板
  2. 点击浏览器后退按钮
    红色弹出式面板将消失
  3. <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>

答案 1 :(得分:0)

试试这个

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