Close pop up on back button

后端 未结 10 1510
长发绾君心
长发绾君心 2020-12-08 08:15

I want to close pop up on click of back button for mobile. I implemented this using onhashchange:

window.onhashchange = function (event) {

};
相关标签:
10条回答
  • 2020-12-08 08:20
    if (window.history && window.history.pushState) {
        $('#myModal').on('show.bs.modal', function (e) {
            window.history.pushState('forward', null, './#modal');
        });
    
        $(window).on('popstate', function () {
            $('#myModal').modal('hide');
        });
    }
    
    0 讨论(0)
  • 2020-12-08 08:23

    I'll did it like this :

    // 2.0 and above
    @Override
    public void onBackPressed(evt) {
        evt.preventDefault();
        closeTab();
    }
    
    // Before 2.0
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            evt.preventDefault();
            closeTab();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

    --> These are handlers btw, need addEventListener =)

    I've done it by memory, i'll check it later when i find it in my 'code mess' folder

    0 讨论(0)
  • 2020-12-08 08:26

    according to http://www.mylearning.in/2015/06/close-modal-pop-up-on-back-button.html

        $('#myModal').on('show.bs.modal', function(e) {
            window.location.hash = "modal";
        });
    
        $(window).on('hashchange', function (event) {
            if(window.location.hash != "#modal") {
                $('#myModal').modal('hide');
            }
        });
    
    0 讨论(0)
  • 2020-12-08 08:28

    This is my solution for bootstrap modals. It adds support closing with back button to all bootstrap modals. You can adapt it for your non-bootstrap popups.

    //Modal Closer With Back Button Support (Uses EventDelegation, so it works for ajax loaded content too.)
    (function() {
        var activeOpenedModalId     = null;
        var isHidingModalByPopState = false;
        $(document).on('show.bs.modal', '.modal', function() {
            activeOpenedModalId  = $(this).attr('id');
            window.location.hash = activeOpenedModalId;
        }).on('hide.bs.modal', '.modal', function() {
            if(!isHidingModalByPopState) {
                window.history.back();
            }
            isHidingModalByPopState = false;
            activeOpenedModalId     = null;
        });
        $(window).on('popstate', function() {
            if(activeOpenedModalId && window.location.hash !== '#'+activeOpenedModalId) {
                isHidingModalByPopState = true;
                $("#" + activeOpenedModalId).modal('hide');
            }
        });
    })();
    
    0 讨论(0)
  • 2020-12-08 08:32

    My Solution in Angular

    // push history state when a dialog is opened
    dialogRef.afterOpened.subscribe((ref: MatDialogRef<any, any>) => {
      window.history.pushState(ref.id, '');
      // pop from history if dialog has not been closed with back button, and gurrent state is still ref.id
      ref.afterClosed().subscribe(() => {
        if (history.state === ref.id) {
          window.history.go(-1);
        }
      });
    });
    
    0 讨论(0)
  • 2020-12-08 08:35

    This could be done easily using Apache Cordova but not sure if you are using it to show your page in webview.

    function onBackKeyDown(e) {
      e.preventDefault();
    
    }
    document.addEventListener("backbutton", onBackKeyDown, false);
    

    http://cordova.apache.org/docs/en/2.4.0/cordova_events_events.md.html#backbutton

    0 讨论(0)
提交回复
热议问题