WebView and popups

psciga

Active Member
Licensed User
Longtime User
Hallo forum,

in a WebView an URL is called and opens a website. It runs fine. This site opens a popup with an html form for collecting data, to send this data to the webserver. In an "orinary" browser (Android/IPhone/PC...) it's no problem.

Please :sign0085: But what to do, to use this popup with B4A?

Thanks in advance - Peter
 

warwound

Expert
Licensed User
Longtime User
If you look at my WebViewSettings library you'll see it has two methods:

getJavaScriptCanOpenWindowsAutomatically
Get if javascript can open windows automatically. The default is false.
Returns true if javascript can open windows automatically during window.open().

setJavaScriptCanOpenWindowsAutomatically
Tell javascript to open windows automatically.
This applies to the javascript function window.open().

So if window.open is being executed then you should be able to set the WebView to allow it to function.

If however you want to enable javascript modal dialogs - that's alert(), prompt() and confirm() - then you'll need my WebViewExtras library.

Use WebViewExtras to add a WebChromeClient to your WebView and the modal dialogs will now be enabled.

Martin.
 

warwound

Expert
Licensed User
Longtime User
Hi.

If i open that link in a desktop browser with javascript enabled then the browser opens the link (https://sin.lido-aero.net/wwwbrfSIA/index.jsp) in a popup window and the browser actually displays this page: https://sin.lido-aero.net/wwwbrfSIA/extra/index.jsp.

If i disable javascript in my desktop browser and open the original link you posted then the browser displays the page that the link refers to (the login form) in the browser (not as a popup) and there is no sign of the other page https://sin.lido-aero.net/wwwbrfSIA/extra/index.jsp.

Now what happens in B4A...?

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim WebView1 As WebView
   Dim WebViewExtras1 As WebViewExtras
   Dim WebViewSettings1 As WebViewSettings
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("WebView1")
   WebView1.JavaScriptEnabled=True
   
   WebViewExtras1.addWebChromeClient(WebView1)
   WebViewSettings1.setJavaScriptCanOpenWindowsAutomatically(WebView1, True)
   
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   
   WebView1.LoadUrl("https://sin.lido-aero.net/wwwbrfSIA/index.jsp")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub WebView1_PageFinished (Url As String)
   Log(Url)
End Sub

You can see that i've enabled javascript for the WebView and also enabled javascript to open new windows.

The log (from an emulator) shows this:


It looks like the WebView loads the login page twice then loads the other page at https://sin.lido-aero.net/wwwbrfSIA/extra/index.jsp

Now with javascript not enabled in the WebView:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("WebView1")
   WebView1.JavaScriptEnabled=False
   
   WebViewExtras1.addWebChromeClient(WebView1)
   '   WebViewSettings1.setJavaScriptCanOpenWindowsAutomatically(WebView1, True)
   
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   
   WebView1.LoadUrl("https://sin.lido-aero.net/wwwbrfSIA/index.jsp")
End Sub

The logon page loads (just once) and displays - there is no sign of the other Lido/Flight Briefing Support Page.

And with javascript enabled but opening of windows disabled:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("WebView1")
   WebView1.JavaScriptEnabled=True
   
   WebViewExtras1.addWebChromeClient(WebView1)
   WebViewSettings1.setJavaScriptCanOpenWindowsAutomatically(WebView1, False)
   
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   
   WebView1.LoadUrl("https://sin.lido-aero.net/wwwbrfSIA/index.jsp")
End Sub

The WebView displays just the text "Please wait..." and the log shows:


A javascript error occurs in the page that contains the login form, here's the source code for that page:

B4X:
<html><head>
  <title>Lido/Flight Briefing Support Page</title>
  <meta http-equiv="pragma"  content="no-cache">
  <meta http-equiv="expires" content="0"       >
  <link rel="stylesheet" href="style.css" type="text/css" title="default">
  <script language="JavaScript">
////////////////////////////////////////////////////////////
// This chunk of JavaScript does a 1-2-3 cha-cha-cha:
// 1. Redirects current window to https: if enabled
// 2. Reloads itself in a (new) window named wwwbrfWin
// 3. Loads extra/index.jsp in current window if not named wwwbrfWin



////////////////////////////////////////////////////////////
  var startUrl    = window.location.href;
  var extraUrl    = "extra/index.jsp";
  var hasHTTPS    = false;
  var wwwbrfWinName = "wwwbrfWin";
  var isNetscape  = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
  var isHTTP      = (window.location.protocol == "http:")  ? true : false;
  var isHTTPS     = (window.location.protocol == "https:") ? true : false;
  var isFile      = (window.location.protocol == "file:")  ? true : false;
  var urlProtocol = window.location.protocol;
  var urlHostname = window.location.hostname;
  var urlPort     = window.location.port;
  var urlPathname = window.location.pathname;
  var urlHash     = window.location.hash;
  var urlSearch   = window.location.search;
  var maxWidth    = screen.availWidth - 9;
  var maxHeight   = screen.availHeight - 49;



////////////////////////////////////////////////////////////
// Step 1 cha-cha-cha
  if (!isFile && isHTTP && hasHTTPS) {
    if (urlPort != "")
      urlPort = parseInt(urlPort) - 80 + 443;
    urlProtocol = "https:";
    startUrl = urlProtocol + "//" + urlHostname + (urlPort != "" ? ":" + urlPort : "")
        "/" + urlPathname + "#" + urlHash + "?" + urlSearch;
  }

  if (startUrl != window.location.href) {
    window.location = startUrl;
  } else {



////////////////////////////////////////////////////////////
// Step 2 cha-cha-cha
    if (window.name != wwwbrfWinName) {
      wwwbrfWin = window.open(startUrl, wwwbrfWinName, ""
          + "location=0,"
          + "menubar=0,"
          + "toolbar=0,"
          + "status=1,"
          + "resizable=0,"
          + "scrollbars=1,"
          + "titlebar=1,"
          + "left=0,"
          + "top=0,"
          + "width=" + maxWidth + ","
          + "height=" + maxHeight
      );



////////////////////////////////////////////////////////////
// Step 3 cha-cha-cha
      document.writeln("<center>Please wait...</center>");
      setTimeout("wwwbrfWin.focus(); document.location = extraUrl;", 500);
    }
  }



////////////////////////////////////////////////////////////
  </script>
</head>
<frameset rows="50,0,250,*" border="0" frameborder="0" framespacing="0" cols="*">

  <frame name="bannerFrame" src="bannerIndex.jsp" frameborder="0" marginwidth="0" marginheight="0" noresize scrolling="no">
  <frame name="actionFrame" src="actionIndex.jsp" frameborder="0" marginwidth="0" marginheight="0" noresize scrolling="no">
  <frame name="loginFrame"  src="loginIndex.jsp"  frameborder="0" marginwidth="0" marginheight="0" noresize scrolling="no">
  <frame name="infoFrame"   src="infoIndex.jsp"   frameborder="0" marginwidth="0" marginheight="0" noresize scrolling="no">
</frameset>
</html>

What is a cha-cha-cha i wonder...?

The actual login form seem to be contained in a frame in that page, the url of that frame is https://sin.lido-aero.net/wwwbrfSIA/loginIndex.jsp.

Maybe you could trying to load just that page that contains the form and see if you can successfully login:

B4X:
Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("WebView1")
   WebView1.JavaScriptEnabled=True
   
   WebViewExtras1.addWebChromeClient(WebView1)
   WebViewSettings1.setJavaScriptCanOpenWindowsAutomatically(WebView1, True)
   
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   
   WebView1.LoadUrl("https://sin.lido-aero.net/wwwbrfSIA/loginIndex.jsp")
End Sub

I've attached my example code to this post.

Martin.
 

Attachments

  • WebViewPopups.zip
    5.6 KB · Views: 424

tcgoh

Active Member
Licensed User
Longtime User
Hi Martin,

Thank you for looking into the problem and coming out with a code.
I have tried the method you used by loading only the login index.jsp, it doesn't work. I have come up with a work around and this is my code.


B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim WebView1 As WebView
   Dim WebView2 As WebView
   Dim Buttonlogin As Button
   Dim WebViewSettings1 As WebViewSettings

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layoutMain")
   Activity.LoadLayout("newlayout")
   WebViewSettings1.SetJavaScriptCanOpenWindowsAutomatically(WebView2,True)
   WebViewSettings1.getJavaScriptCanOpenWindowsAutomatically(WebView2)
   
   WebView2.LoadUrl("https://sin.lido-aero.net/wwwbrfSIA/index.jsp")
End Sub

Sub Buttonlogin_Click
   WebView1.LoadUrl("https://sin.lido-aero.net/wwwbrfSIA/")
   Buttonlogin.Visible = False
End Sub

I used a button to click after the support page was laoded and the login works.

Thanks
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…