konstantinbueschel
10/15/2014 - 11:23 AM

Convert HTML Links to Titanium Mobile App Links that fire Ti.App.Events

Convert HTML Links to Titanium Mobile App Links that fire Ti.App.Events

<?php

// Event-Handler für Titanium-Framework
$sText = preg_replace('~<a href="((http|https)[^"]+)"[^>]*>(.*?)</a>~si', '<a href="\\1" onclick="Ti.App.fireEvent(\'app:webViewClick\', {link: \'\\1\'}); return false;">\\3</a>', $sText);
  
// mailto spezial
$sText = preg_replace('~<a href="mailto:([^"]+)"[^>]*>(.*?)</a>~si', '<a href="mailto:\\1" onclick="Ti.App.fireEvent(\'app:webViewClick\', {email: \'\\1\'}); return false;">\\2</a>', $sText);

?>
var convertedHTML = htmlString;

// event-handler for app
convertedHTML = htmlString.replace(/<a href="((http|https)[^"]+)"[^>]*>(.*?)<\/a>/gmi, '<a href="$1" onclick="Ti.App.fireEvent(\'app:webViewClick\', {link: \'$1\'}); return false;">$3</a>');

// mailto special
convertedHTML = convertedHTML.replace(/<a href="mailto:([^"]+)"[^>]*>(.*?)<\/a>/gmi, '<a href="mailto:$1" onclick="Ti.App.fireEvent(\'app:webViewClick\', {linkType: \'email\', link: \'$1\'}); return false;">$2</a>');