Difference between revisions of "Titanium webview"
From John Freier
| (6 intermediate revisions by the same user not shown) | |||
| Line 5: | Line 5: | ||
For IOS setting the height of a WebView can be tough. A good trick is to get the height of the HTML document and setting the WebView height. | For IOS setting the height of a WebView can be tough. A good trick is to get the height of the HTML document and setting the WebView height. | ||
| − | var actualHeight = e.source.evalJS("document.body.offsetHeight"); | + | $.webView.addEventListener('load', function() { |
| − | + | var actualHeight = e.source.evalJS("document.body.offsetHeight"); | |
| + | e.source.height = actualHeight; | ||
| + | }); | ||
| Line 15: | Line 17: | ||
var webViewBeforeLoad = function(e) { | var webViewBeforeLoad = function(e) { | ||
| − | var containsFile = | + | var containsFile = URLContainsFilePrefix(e.url || ''); |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
// Parse URLs from the news feed that don't contain 'http://' | // Parse URLs from the news feed that don't contain 'http://' | ||
if (containsFile && e.url.indexOf("www")>0) | if (containsFile && e.url.indexOf("www")>0) | ||
e.url = "http://" + e.url.substring(e.url.indexOf("www"), e.url.length); | e.url = "http://" + e.url.substring(e.url.indexOf("www"), e.url.length); | ||
| − | containsFile = | + | containsFile = URLContainsFilePrefix(e.url || ''); |
// Need for Android. On Android the initial load of a WebView attempt is 'file://' | // Need for Android. On Android the initial load of a WebView attempt is 'file://' | ||
| Line 37: | Line 35: | ||
// Load event for a WebView for outside links. | // Load event for a WebView for outside links. | ||
function webViewLoad(e) { | function webViewLoad(e) { | ||
| − | + | ||
| − | + | var containsFile = URLContainsFilePrefix(e.url || ''); | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | var containsFile = | + | |
// Parse URLs from the news feed that don't contain 'http://' | // Parse URLs from the news feed that don't contain 'http://' | ||
| Line 49: | Line 42: | ||
e.url = "http://" + e.url.substring(e.url.indexOf("www"), e.url.length); | e.url = "http://" + e.url.substring(e.url.indexOf("www"), e.url.length); | ||
| − | containsFile = | + | containsFile = URLContainsFilePrefix(e.url || ''); |
// Need for Android. On Android the initial load of a WebView attempt is 'file://' | // Need for Android. On Android the initial load of a WebView attempt is 'file://' | ||
| Line 55: | Line 48: | ||
if (!containsFile && $.alertsTitle.canGoBack() && !Alloy.Globals.IsMobileWeb) | if (!containsFile && $.alertsTitle.canGoBack() && !Alloy.Globals.IsMobileWeb) | ||
$.alertsTitle.goBack(); | $.alertsTitle.goBack(); | ||
| + | }; | ||
| + | |||
| + | // Load links from the webview in an outside browser. | ||
| + | $.webView.addEventListener('beforeload', webViewBeforeLoad); | ||
| + | |||
| + | // Load links from the webview in an outside browser. Belive this is an Android specific item. | ||
| + | $.webView.addEventListener('load', webViewLoad); | ||
| + | |||
| + | function URLContainsFilePrefix(url) { | ||
| + | var match = url.match(/^file:\/\//); | ||
| + | if (match && typeof match.index != undefined) { | ||
| + | return true; | ||
| + | } | ||
| + | return false; | ||
}; | }; | ||
Latest revision as of 09:55, 10 January 2014
Setting the height
IOS
For IOS setting the height of a WebView can be tough. A good trick is to get the height of the HTML document and setting the WebView height.
$.webView.addEventListener('load', function() {
var actualHeight = e.source.evalJS("document.body.offsetHeight");
e.source.height = actualHeight;
});
Clickable Links
This is to override the links inside a WebView.
var webViewBeforeLoad = function(e) {
var containsFile = URLContainsFilePrefix(e.url || );
// Parse URLs from the news feed that don't contain 'http://'
if (containsFile && e.url.indexOf("www")>0)
e.url = "http://" + e.url.substring(e.url.indexOf("www"), e.url.length);
containsFile = URLContainsFilePrefix(e.url || );
// Need for Android. On Android the initial load of a WebView attempt is 'file://'
if (Alloy.Globals.IsMobileWeb || containsFile)
return;
Ti.Platform.openURL(e.url);
$.alertsTitle.stopLoading();
};
// Load event for a WebView for outside links.
function webViewLoad(e) {
var containsFile = URLContainsFilePrefix(e.url || );
// Parse URLs from the news feed that don't contain 'http://'
if (containsFile && e.url.indexOf("www")>0)
e.url = "http://" + e.url.substring(e.url.indexOf("www"), e.url.length);
containsFile = URLContainsFilePrefix(e.url || );
// Need for Android. On Android the initial load of a WebView attempt is 'file://'
// Check if the WebView can go back if loaded from an outside link.
if (!containsFile && $.alertsTitle.canGoBack() && !Alloy.Globals.IsMobileWeb)
$.alertsTitle.goBack();
};
// Load links from the webview in an outside browser.
$.webView.addEventListener('beforeload', webViewBeforeLoad);
// Load links from the webview in an outside browser. Belive this is an Android specific item.
$.webView.addEventListener('load', webViewLoad);
function URLContainsFilePrefix(url) {
var match = url.match(/^file:\/\//);
if (match && typeof match.index != undefined) {
return true;
}
return false;
};