Capturing events from the back using web browsing

I am new to web browsing and trying to use the app. I have a popup that displays using javascript. He has a closed button to close. Along with the close button, I want to use the built-in return button.

That is, if the user clicks the "Back" button, my popup should be closed.

My doubt is whether any changes require from my own application? Or do webviews convert the action of the back button to some events, such as keystrokes, that a webview can understand?

+5
source share
5 answers

I don't know if you have HTML5 support, but if history.pushState and history.onpopstate from the history API will do exactly what you want.

When the pop-up window is displayed, use pushState to inform the browser about the new history entry, just as if the pop-up window was instead of a new page.

Then add an event listener to history.onpopstate that closes the popup if it is open.

+2
source

You can try to mix some questions and their solutions:

+2
source

If you use Cordoba, it offers an event to track the back button on Android devices:

 document.addEventListener("backbutton", yourCallbackFunction, false); 

You can override this event and implement your own behavior. In addition, there should be no way to track the back button event.

+1
source

Clicking on the button will close your viewcontroller, which shows the modal.

I think you do not want this to happen, and just close the javascript modal.

Yes, you will need to change your own code.

 On native backbutton click, if your modal is open: close the modal else: do default backbutton action. 

The following is a detailed explanation.

 On native backbutton click # your native code to intercept the back button if your modal is open: # there are multiple options here # first strategy # let javascript inform native side when it opens/closes popup # native just needs to check the status he has # second strategy # native asks javascript if he has a modal open # this is a bit tricky because native can't get a return value from javascript # native asks javascript to tell native if he has a modal open (native->javascript + javasciprt -> native two function calls) # native checks what javascript just said close the modal # this is easy, ask javascript to close it. 

I assumed that you know how to communicate between javascript and native, if you do not look at http://www.smashingmagazine.com/2013/10/best-of-both-worlds-mixing-html5-native-code/

+1
source

To help what lifeisfoo says:

 public class WebViewer WebView webView; @Override public void onBackPressed() { if (webView.hasPopUp()) { webView.loadUrl("javascript:closePopUp()"); } else { super.onBackPressed(); } } 
+1
source

All Articles