Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open an external link in Safari (cordova)

I am trying to open an external url link in my app which is a cordova app. Right now its presents an in app browser using modal view but it has NO back button or close button. The user essentially gets stuck when they click an external link. For instance, when someone clicks this link, enclosed in "Visit Website", an in app browser shows up, the website shows up fine, BUT there is no way to navigate back to the app, or close the in app browser. How do I go about fixing this?

<a href="http://www.sdtaproom.com/" target="_blank">Visit Website</a>

I saw that there is a solution, window.open("http://stackoverflow.com", "_system");, but I don't know how to implement it in the href code.

ANSWER (Edited): Add this code in the script tag in the head.

<script src="cordova.js"></script>
<script type="text/javascript">
window.addEventListener('load', function () {
  $(document).on('click', 'a[target="_system"],a[target="_blank"]', function (e) {
    e.preventDefault();
    var url = this.href;
    window.open(url,"_system");
  });
}, false);
</script>
like image 285
Josh O'Connor Avatar asked Jul 16 '15 18:07

Josh O'Connor


1 Answers

You can embed javascript code in the href attribute. This should do the trick:

<a href="javascript: window.open('http://www.sdtaproom.com/', '_system'); return false;">Visit Website</a>

You will also have to install the InAppBrowser plugin (don't be fooled by its name).

like image 102
Glorfindel Avatar answered Sep 17 '22 17:09

Glorfindel