Opening an external browser from app

Tip : If you’re pasting code, html or config files, surround the code with triple back ticks (```), before the first line and after the last one. It will be formatted properly. (We fixed it for you this time)

1 Like

Yes, that’s how JavaScript/End JavaScript works.

(BTW - no need to call Main() in your code - it’s done automatically by AppStudio. Your code will call it twice)

Yes but it throws an error:

That’s because the code you copied is not syntactically correct - there’s probably a bit more to the code where you got it from.

Perhaps you’re missing the word “function” just before the openUrl(…

OK, I’ve figured it out with lots of patient help from George and the community.

  1. Add the Cordova InAppBrowser to the config.xml file
  2. Add a chunk of JavaScript to my code. JS is case-sensitive as I learned the hard way:
JavaScript
function openUrl(webURL) {
    if (!window.cordova) { 
       window.open(webURL, "_system");
     } else {
       cordova.InAppBrowser.open(webURL, "_system");
    }
  }
End JavaScript
  1. Use this span in my htmlView control to show an underlined blue hyperlink with the pointer cursor while hovering over it:
<span style="text-decoration:underline; color:blue; cursor:pointer" onclick="openUrl('https://www.escardio.org')">European Society of Cardiology)</span>

This has the desired effect of opening a new browser instance from the iOS app version or a new tab from the browser version. The app version includes the little back arrow to my app in the upper left corner.

This victory comes not quite 50 years after writing my first BASIC program in 1976 at our community college at the age of 15!

2 Likes

Nicely done!

1 Like

Hooray - good work!

1 Like

Unfortunately it stopped working for some reason. Clicking on the link below does not seem to trigger the onclick event anymore and run the openUrl function:

<li>Istituto Superiore di Sanit&agrave;, EpiCentro: <span style="text-decoration:underline; color:blue; cursor:pointer" onclick="openUrl('https://www.epicentro.iss.it/vaccini/calendario')">Calendario vaccinale</span></li>

Here is the Javascript chunk:

Sub Main()
  
Dim webURL

JavaScript
function openUrl(webURL) {
    if (!window.cordova) { 
       window.open(webURL, "_system");
     } else {
      cordova.InAppBrowser.open(webURL, "_system");
    }
  }
End JavaScript

dropTopic.hidden = True
dropTopicItalian.hidden = False

End Sub

Any ideas? I have tried moving the Dim webURL to the Project properties code window but that didn’t work.

I see your function openURL() is within Sub Main(), which is also a function. Since it’s inside of Sub Main(), its scope is restricted to code which is within Sub Main().

Try moving it above Sub Main()… End Sub

That fixed it, thanks so much.