PhoneGap/Cordova 2.3.: how to open all external links in InAppBrowser?

末鹿安然 提交于 2019-12-08 00:13:59

问题


I am developing a mobile app using Sencha Touch 2 and Cordova 2.3.0. I would like to be able to open all external links (from dynamically loaded HTML) into the new InAppBrowser.

Is there a way to achieve this without having to modify all external links to contain target="_blank"?

I would like to intercept clicks on external links and open them using the window.open API from InAppBrowser. Since I target both iOS and Android, I guess a javascript solution would be better to avoid separate codes (Java and Objective-C).

Thanks!

update: I just found this: https://gist.github.com/4694032

The only trouble with this is that I don't use jQuery in my app. Is it worth it to include it just for this matter?


回答1:


Ext.each(Ext.query('a'), function(el) {
    el = Ext.get(el);
    el.on('click', function(e) {
        e.preventDefault();
        console.log('clicked', el.getAttribute('href'));
    });
});



回答2:


You can also add this event exclusively for attributes(href) that begins with "http", using regexp:

Ext.Viewport.element.dom.addEventListener('click', function (e) {
    if (e.target.tagName !== 'A') {
        return;
    };

    var url = e.target.getAttribute('href');
    var containsHttp = new RegExp('http\\b'); 

    //if href value begins with 'http'
    if(containsHttp.test(url)) { 
        e.preventDefault();
        window.open(url, "_system"); // For iOS
        navigator.app.loadUrl(url, {openExternal: true}); //For Android
    }
    else {
        return;
    }
}, false);

Then you can build for android and iOS at the same time.



来源:https://stackoverflow.com/questions/14664425/phonegap-cordova-2-3-how-to-open-all-external-links-in-inappbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!