diff --git a/www/core/directives/iframe.js b/www/core/directives/iframe.js index bd70dd789a4..e97f2a16fef 100644 --- a/www/core/directives/iframe.js +++ b/www/core/directives/iframe.js @@ -34,18 +34,22 @@ angular.module('mm.core') /** * Intercept window.open in a frame and its subframes, shows an error modal instead. + * Search links () and open them in browser or InAppBrowser if needed. * * @param {DOMElement} element Element to treat. * @return {Void} */ - function interceptPopups(element) { + function treatFrame(element) { if (element) { // Redefine window.open in this element and sub frames, it might have been loaded already. redefineWindowOpen(element); + // Treat links. + treatLinks(element); element.on('load', function() { - // Element loaded, redefine window.open again. + // Element loaded, redefine window.open and treat links again. redefineWindowOpen(element); + treatLinks(element); }); } } @@ -96,11 +100,41 @@ angular.module('mm.core') // Search sub frames. angular.forEach(tags, function(tag) { angular.forEach(contents.find(tag), function(subelement) { - interceptPopups(angular.element(subelement)); + treatFrame(angular.element(subelement)); }); }); } + /** + * Search links () and open them in browser or InAppBrowser if needed. + * + * @param {DOMElement} element Element to treat. + * @return {Void} + */ + function treatLinks(element) { + var links = element.contents().find('a'); + angular.forEach(links, function(el) { + var href = el.href; + + // Check that href is not null. + if (href) { + if (href.indexOf('http') === 0) { + // Link has protocol http(s), open it in browser. + angular.element(el).on('click', function(e) { + e.preventDefault(); + $mmUtil.openInBrowser(href); + }); + } else if (el.target == '_parent' || el.target == '_top' || el.target == '_blank') { + // Opening links with _parent, _top or _blank can break the app. We'll open it in InAppBrowser. + angular.element(el).on('click', function(e) { + e.preventDefault(); + $mmUtil.openInApp(href); + }); + } + } + }); + } + return { restrict: 'E', template: '
', @@ -112,18 +146,7 @@ angular.module('mm.core') scope.height = $mmUtil.formatPixelsSize(attrs.iframeHeight) || '100%'; var iframe = angular.element(element.find('iframe')[0]); - interceptPopups(iframe); - iframe.on('load', function() { - angular.forEach(iframe.contents().find('a'), function(el) { - var href = el.getAttribute('href'); - if (href && href.indexOf('http') === 0) { // Check that href is not null. - angular.element(el).on('click', function(e) { - $mmUtil.openInBrowser(href); - e.preventDefault(); - }); - } - }); - }); + treatFrame(iframe); } };