繁体   English   中英

PhoneGap / Cordova 2.3 .:如何在InAppBrowser中打开所有外部链接?

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

我正在使用Sencha Touch 2和Cordova 2.3.0开发移动应用程序。 我希望能够将所有外部链接(从动态加载的HTML)打开到新的InAppBrowser中。

有没有一种方法可以实现而无需修改所有外部链接以包含target =“ _ blank”?

我想拦截外部链接上的点击,并使用InAppBrowser中的window.open API打开它们。 由于我同时针对iOS和Android,因此我认为使用JavaScript解决方案最好避免使用单独的代码(Java和Objective-C)。

谢谢!

更新:我刚刚找到了这个: https : //gist.github.com/4694032

唯一的麻烦是我没有在应用程序中使用jQuery。 仅仅为此就包括它是否值得?

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

您还可以使用regexp仅针对以“ http”开头的attribute(href)添加此事件:

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);

然后,您可以同时为android和iOS进行构建。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM