Difference between revisions of "Titanium webview"

From John Freier
Jump to: navigation, search
Line 7: Line 7:
 
   var actualHeight = e.source.evalJS("document.body.offsetHeight");
 
   var actualHeight = e.source.evalJS("document.body.offsetHeight");
 
   e.source.height = actualHeight;
 
   e.source.height = actualHeight;
 +
 +
 +
 +
== Clickable Links ==
 +
 +
This is to override the links inside a WebView.
 +
 +
  var webViewBeforeLoad = function(e) {
 +
  var containsFile = Alloy.Globals.newsService.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 = Alloy.Globals.newsService.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) {
 +
 
 +
  if (Alloy.Globals.IsIPhone) {
 +
  var actualHeight = e.source.evalJS("document.body.offsetHeight");
 +
  e.source.height = actualHeight;
 +
  }
 +
 
 +
  var containsFile = Alloy.Globals.newsService.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 = Alloy.Globals.newsService.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();
 +
  };

Revision as of 16:41, 9 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.

 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 = Alloy.Globals.newsService.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 = Alloy.Globals.newsService.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) {
 	
 	if (Alloy.Globals.IsIPhone) {
 		var actualHeight = e.source.evalJS("document.body.offsetHeight");
 		e.source.height = actualHeight;
 	}
 	
 	var containsFile = Alloy.Globals.newsService.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 = Alloy.Globals.newsService.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();
 };