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

标签 javascript jquery html web browser

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

任何人都可以帮我解释为什么打开对话框时不发送 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);
    });

最佳答案

试试这个

假设点击popup-btn,将显示popup-panel

要采取的行动

  1. 点击打开弹出面板链接。
    将显示红色弹出面板
  2. 点击浏览器后退按钮
    红色弹出面板将消失

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

关于javascript - 当用户按下浏览器的后退按钮时关闭 Jquery 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23685304/

相关文章:

javascript - 仅当选中单选按钮时才需要输入字段 - Angular 4

javascript - 如果子数组的长度大于零,则显示对象

javascript - 具有交替滚动边的视差分屏

javascript - 使用 knockout.js 填充 django 模板中的表

jquery - jQuery 中的“this”对象

jquery - 使用 jQuery 拖放 UI 布局

javascript - JS Analytics 电子商务回调

javascript - 清除具有特定前缀的本地存储值

javascript - 如何设置加载动态内容的 onClick 属性?

php - JQuery 帖子不起作用问题