Skip to content

Instantly share code, notes, and snippets.

@rewonc

rewonc/app.js

Last active Apr 3, 2018
Embed
What would you like to do?
AngularJS/PhoneGap/Ionic: filter to convert links for opening in separate mobile window
//*
//* See the JS Fiddle: http://jsfiddle.net/sxcjmoj5/3/
//*
//*
// make sure ngSanitize module is installed:
// https://docs.angularjs.org/api/ngSanitize
//
// To convert links from plaintext, you must use the linky filter which is included with ngSanitize
// https://docs.angularjs.org/api/ngSanitize/filter/linky
//
// You might need to install inappbrowser if you haven't done that:
// https://github.com/apache/cordova-plugin-inappbrowser/blob/master/doc/index.md
//
// In your app.js
var myApp = angular.module('myApp', ['ngSanitize']);
myApp.filter('hrefToJS', function ($sce, $sanitize) {
return function (text) {
var regex = /href="([\S]+)"/g;
var newString = $sanitize(text).replace(regex, "onClick=\"window.open('$1', '_blank', 'location=yes')\"");
return $sce.trustAsHtml(newString);
}
});
myApp.controller('MyCtrl', function ($scope) {
$scope.html = "This a link: <a href='https://www.google.com'>Google</a> :)";
$scope.plaintext = "This is a link: https://www.google.com :) "
});
///Usage in your HTML template
/*
<div ng-app="myApp">
<div ng-controller="MyCtrl">
<h1>Before</h1>
<p ng-bind-html="html"></p>
<p ng-bind-html="plaintext"></p>
<h1>After</h1>
<p ng-bind-html="html | hrefToJS"></p>
<p ng-bind-html="plaintext | linky | hrefToJS"></p>
</div>
</div>
*/
@olore

This comment has been minimized.

Copy link

@olore olore commented Nov 19, 2014

Thanks!

@bairamispanagiotis

This comment has been minimized.

Copy link

@bairamispanagiotis bairamispanagiotis commented Jan 18, 2015

You are a little God!
thanks!

@renanaraujo

This comment has been minimized.

Copy link

@renanaraujo renanaraujo commented Jan 23, 2015

Thanks man!!!!

Do you know how to open in browser instead a new page over the app?

@clawish

This comment has been minimized.

Copy link

@clawish clawish commented Feb 10, 2015

works beautifully!
@renanaraujo: yes, replace _blank with _system.

@ashishvshah

This comment has been minimized.

Copy link

@ashishvshah ashishvshah commented Mar 4, 2015

@clawfish: i have tried the _system notation with no luck. Still opens in app on an android device. here is my snippet, any ideas?

ng-bind-html="contents | linky: '_system' "

@UrVerySpecial

This comment has been minimized.

Copy link

@UrVerySpecial UrVerySpecial commented Aug 17, 2015

very helpful, Thanks a lot!

@oculos

This comment has been minimized.

Copy link

@oculos oculos commented Nov 9, 2015

Very helpful! I am using it for non-mobile pages, and it works good. However, is there a way to open on a new tab instead?

@tatu89

This comment has been minimized.

Copy link

@tatu89 tatu89 commented Dec 16, 2015

Thanks a lot for this code! It works perfect :)
I've just a little issue with the links in my app. When you remove the href with your regex the link is not marked as a link anymore, it looks just like normal text. Even though you can click on it and it will open in the device standard browser as it should. But the users might not recognize it as link.

So I added href="#" to your replace string:
var newString = $sanitize(text).replace(regex, "href="#" onClick="window.open('$1', '_blank', 'location=yes')"");

Now there is somehow a weird behaviour. If there is only one link in the html text it works fine. The link opens a new browser window.
If there are two or more links it still works for the first click on any of the links. As soon as I click another link my app is still opening the link but switching to the home screen of my app.
Do you have any idea how to solve that? Or is there a better way to mark it as a clickable link instead of adding href="#"?

@esharksolutions

This comment has been minimized.

Copy link

@esharksolutions esharksolutions commented Jan 11, 2016

Great start for me, but can't get it to cross the finish line . . .
I want to open the external link in a web browser OUTSIDE the web app. I tried changing '_system' to '_blank' with no success.
Any other suggestions?

@niyando

This comment has been minimized.

Copy link

@niyando niyando commented Jan 26, 2016

This works great. I suggest a small edit.

var newString = $sanitize(text).replace(regex, "href onClick=\"window.open('$1', '_blank', 'location=yes');return false;\"");

Adding href preserves anchor styling and return false to prevent default click.

@tatu89

This comment has been minimized.

Copy link

@tatu89 tatu89 commented Feb 1, 2016

@niyando: Perfect! Thank you so much :)

@ChandruMgr

This comment has been minimized.

Copy link

@ChandruMgr ChandruMgr commented Feb 24, 2016

hello, can any one help me...
am new to angularjs and phone as well as phonegap..
i have built app for android in ionic framework, the link which i would gave it download the page but when i installed in android mobile it will not work.
where i am wrong.
plz suggest me..

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 17, 2016

great

@clintgh

This comment has been minimized.

Copy link

@clintgh clintgh commented Apr 28, 2016

Perfect!
I love this community.

@lucaslimasouza

This comment has been minimized.

Copy link

@lucaslimasouza lucaslimasouza commented Apr 28, 2016

Thanks. Good works.

@renanss

This comment has been minimized.

Copy link

@renanss renanss commented Jun 3, 2016

Thank you.

@brunosserrao

This comment has been minimized.

Copy link

@brunosserrao brunosserrao commented Jun 9, 2016

It's saved my life.Thank you so much.

@adyba

This comment has been minimized.

Copy link

@adyba adyba commented Jun 29, 2016

Even the "naked" ngSanitize offers something like this https://code.angularjs.org/1.4.12/docs/api/ngSanitize/filter/linky
Once you need something more advanced (hashtags, mentions) take a look at https://github.com/scottcorgan/angular-linkify

@analistacarlosh

This comment has been minimized.

Copy link

@analistacarlosh analistacarlosh commented Jul 6, 2016

Thaks.

@J261

This comment has been minimized.

Copy link

@J261 J261 commented Oct 15, 2016

perfect

@tkserver

This comment has been minimized.

Copy link

@tkserver tkserver commented Jun 14, 2017

I've got this working on android after building an angular 1.5 app through ionic/cordova build. But on iOS the outgoing link does not open safari. It goes to the app's home page, which I'm assuming is the "otherwise" part of the routes kicking in. I've got inappbrowser installed. Perhaps that's not configured correctly?

@pbnelson

This comment has been minimized.

Copy link

@pbnelson pbnelson commented Aug 24, 2017

Thanks for this! My Ionic1 app doesn't open links in the browser (webview) mode when target=_blank, instead it requires target=_system. I adapted the code slightly and now it works for me in both browser mode and iOS. If you're having the same problem, try this:

        var newString = $sanitize(text).replace(regex, "href onClick=\"window.open('$1', '" + (ionic.Platform.isWebView()) ? '_system' : '_blank' + "', 'location=yes');return false;\"");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment