[英]PhoneGap/Cordova 2.3.: how to open all external links in InAppBrowser?
I am developing a mobile app using Sencha Touch 2 and Cordova 2.3.0. 我正在使用Sencha Touch 2和Cordova 2.3.0开发移动应用程序。 I would like to be able to open all external links (from dynamically loaded HTML) into the new InAppBrowser.
我希望能够将所有外部链接(从动态加载的HTML)打开到新的InAppBrowser中。
Is there a way to achieve this without having to modify all external links to contain target="_blank"? 有没有一种方法可以实现而无需修改所有外部链接以包含target =“ _ blank”?
I would like to intercept clicks on external links and open them using the window.open API from InAppBrowser. 我想拦截外部链接上的点击,并使用InAppBrowser中的window.open API打开它们。 Since I target both iOS and Android, I guess a javascript solution would be better to avoid separate codes (Java and Objective-C).
由于我同时针对iOS和Android,因此我认为使用JavaScript解决方案最好避免使用单独的代码(Java和Objective-C)。
Thanks! 谢谢!
update: I just found this: https://gist.github.com/4694032 更新:我刚刚找到了这个: https : //gist.github.com/4694032
The only trouble with this is that I don't use jQuery in my app. 唯一的麻烦是我没有在应用程序中使用jQuery。 Is it worth it to include it just for this matter?
仅仅为此就包括它是否值得?
Ext.each(Ext.query('a'), function(el) {
el = Ext.get(el);
el.on('click', function(e) {
e.preventDefault();
console.log('clicked', el.getAttribute('href'));
});
});
You can also add this event exclusively for attributes(href) that begins with "http", using regexp: 您还可以使用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);
Then you can build for android and iOS at the same time. 然后,您可以同时为android和iOS进行构建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.