Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

ios - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...