Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle back button on Android devices #197

Closed
elifares opened this issue Jan 30, 2017 · 2 comments · May be fixed by #229
Closed

Handle back button on Android devices #197

elifares opened this issue Jan 30, 2017 · 2 comments · May be fixed by #229

Comments

@elifares
Copy link

Hi,

This works great on my website but when the modal is opened and viewed on an Android device and then the user taps their back button, the mobile browser leaves/closes the website.

To reproduce, open jquery-modal on an Android phone and then tap the back button.
Is there any way the back button can just close the modal?

Thanks!

@gorillamoe
Copy link
Collaborator

That is how browsers work, when the URL does not change, when you open the modal window, it will call history.back().

You can work around that, by doing something like this:

HTML

<button>Click to view the video</button>

JavaScript

(function(window, document){
$(window).on('popstate', function() {
  if(window.location.hash === '#kylefoxrocks') {
    $('.jquery-modal').remove();
  }
});
$('button').on('click', function(evt) {
	evt.preventDefault();
	window.location.href = '#kylefoxrocks'
	window.location.href = '#modalopened'
	var video = document.createElement('video');
	var src = document.createElement('source');
	video.setAttribute('controls', '');
	src.setAttribute('src', 'https://cdn.emkae.net/magic-mike-xxl-i-want-it-that-way.mp4');
	src.setAttribute('type', 'video/mp4');
	video.appendChild(src);
	$(video).appendTo('body').modal();
});
})(window, document);

See the Demo
Demo Source Code

@Gillesvk
Copy link

Gillesvk commented Aug 9, 2018

I have the same issue using Kyle Fox modal in my single page website where I load external content in full screen modal. The user leaves website when he taps browser back button instead of modal close button.

I try to resolving it by manipulating the history API with Jquery.
In the example code bellow, "window.history.pushstate" and "window.history.back" methods works well when I click the modal open and close button, but I can't figure out how to handle browser back and forward button to make modal reopen on forward and close on back. It's something to do with "popstate".

Any help ?

Here is an example code:

Index page:

<a href="ajax-content.html" data-modal>Load ajax content</a>

Content load in modal (ajax-content.html):

<p>I'm ajax content.</p>
<a href="#" id="closemodal">Close</a>

Jquery:

<script>

// Open modal and history pushstate
$('a[data-modal]').click(function(event) {
    $(this).modal();
    window.history.pushState(null, null, this.href);
    return false;
});

// Close modal and history back
$("body").on('click', '#closemodal', function () {
    $.modal.close();
    window.history.back();
    return false;
});

// Browser back and forward button   
$(window).on('popstate', function () {
    ... ???
});

</script>

Any help would be most welcome ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants