问题
I have a webview in my application and I also have a website online. I have loaded my website in my webview and when I press a link in my website I want to open that link in Safari and not in my webview, here´s what I have done.
I have my webview:
var webview = Titanium.UI.createWebView({
url: 'http://www.TEST.com', // dummy url now
top: 42,
left: 0,
width: 310,
height: 275
});
My HTML file (which I will put online so I will only access it trough my www address):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="img.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="script.js"></script>
</head>
<body onload="Ti.API.info('body loaded!');">
<div id="wrapper">
<ul class="slide" id="slideOne">
<li><img src="images/1.jpg" onClick="Ti.App.fireEvent('openlink',{url:'http://www.google.com'})"/></li>
</div>
</body>
</html>
And to access the HTML code I have the following code::
Ti.App.addEventListener('openlink', function(e) {
alert('Ti.App);
Ti.Platform.openURL(e.url);
});
But nothing happens when I press the image, anyone has a clue why?
回答1:
You cant do this on a remote web site.
From this guide, in the Remote web content section:
You cannot use any Titanium statements within HTML content loaded from a remote host.
回答2:
Aaron Saunders was real close. Not bad for just typing it up on the fly! I've modified it to use beforeload and instead of using a string check, I use a property from the beforeload event (e).
$.webView.addEventListener('beforeload', function(e) {
if (e.navigationType == Titanium.UI.iOS.WEBVIEW_NAVIGATIONTYPE_LINK_CLICKED) {
// stop the event
e.bubble = false;
// stop the url from loading
$.webView.stopLoading();
// open in safari
Ti.Platform.openURL(e.url);
}});
回答3:
you can try to intercept the loading of the new url in your application and redirect to a new safari instance.
this code is not tested... i just typed it up here to give you a general idea
// if i have http then assume external url
$.webView.addEventListener('load', function(e) {
if (e.url.indexOf("http") !== -1) {
// stop the event
e.bubble = false;
// stop the url from loading
$.webView.stopLoading();
// open in safari
Ti.Platform.openURL(e.url);
}
}
回答4:
Yikes, these answers are so wrong.
The Titanium.UI.Webview has a specific property for intercepting links: onlink
.
This is not implemented as an event because it is a callback and needs to return a boolean to tell the Webview whether or not to load the URL of the link.
Oddly, setting the onlink
callback right away makes the URL immediately load in Safari, so I did it this way:
$.webview.addEventListener('load', function(e) {
$.webview.onlink = function(e) {
Ti.Platform.openURL(e.url);
return false;
};
});
You can of course check the e.url
string and decide whether to open it internally or externally.
来源:https://stackoverflow.com/questions/18026449/get-url-from-remote-url-in-webview-and-open-it-in-safari