本文介绍了使用Titanium的webview引发API事件时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Titanium的webview中打开的外部HTML页面触发事件。



app.js文件... >

  var组,现在,选项卡,视图,窗口; 

现在=新的Date();
view = Titanium.UI.createWebView({url:‘http://MYWEBSITE.com/index.htm?time=’+ now.getTime()});

window = Titanium.UI.createWindow({tabBarHidden:true,navBarHidden:true});
window.add(view);

Titanium.App.addEventListener('browse',function(e){
Ti.API.info(我从网络视图中收到 + e.something +。);
});

组= Titanium.UI.createTabGroup();
标签= Titanium.UI.createTab({标题:窗口,窗口:窗口});
group.addTab(tab);
group.open(tab);

js摘录自网页...

  $(#testButton)。mousedown(function(){
警报(我被点击了。);
Ti.App.fireEvent('browse',{something:'stuff'});
});






(我在网址中添加了时间,以确保页面总是新鲜的。)



如上所示添加事件侦听器,或使用view.addEventListener进行编译,但最终不起作用。



使用Titanium.UI.WebView.addEventListener会产生一条错误消息,指出该对象不存在。



我是否需要打开



还可以,因为Titanium.App.fireEvent是除Titanium之外的公认功能,如何防止JavaScript错误? / p>

谢谢。

解决方案
  //来自网页
<!DOCTYPE html PUBLIC-// W3C // DTD HTML 4.01 // EN
http://www.w3.org/TR/html4/strict。 dtd>

< html lang = zh-CN>
< head>
<元http-equiv = Content-Type content = text / html; charset = utf-8>
< / head>
< body>
< div id ='testButton'>测试按钮< / div>
< / body>

< script>
var _button = document.getElementById( testButton);
_button.onmousedown =函数(){
警报(this.id);
Ti.App.fireEvent('fromwebview',{name:this.id});
返回false;
};

< / script>
< / html>

来自apps.js

  Ti.App.addEventListener('fromwebview',function(data)
{
Titanium.API.info(-> + data.name);
});


I'm trying to fire an event from an external HTML page opened inside of Titanium's webview.

app.js file...

var group, now, tab, view, window;

now = new Date();
view = Titanium.UI.createWebView({url: 'http://MYWEBSITE.com/index.htm?time=' + now.getTime()});

window = Titanium.UI.createWindow({tabBarHidden: true, navBarHidden: true});
window.add(view);

Titanium.App.addEventListener('browse', function(e) {
    Ti.API.info("I received " + e.something + " from the webview.");
});

group = Titanium.UI.createTabGroup();
tab = Titanium.UI.createTab({title: 'window', window: window});
group.addTab(tab); 
group.open(tab);

js excerpt from web page...

$("#testButton").mousedown(function() {
    alert ("I got clicked.");
    Ti.App.fireEvent('browse', {something:'stuff'});
});


(I include the time in the URL to ensure the page is always fresh.)

Adding the event listener as shown above, or using view.addEventListener, compiles but ultimately doesn't work.

Using Titanium.UI.WebView.addEventListener produces an error message that the object doesn't exist.

Do I need to open the URL/webview in a different manner?

Also, since Titanium.App.fireEvent is not a recognized function, except to Titanium, how does one prevent a JavaScript error?

Thanks.

解决方案
// from web page
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
    <div id='testButton'>TEST BUTTON</div>
</body>

<script>
    var _button = document.getElementById ("testButton");
    _button.onmousedown = function () {
        alert (this.id);
        Ti.App.fireEvent('fromwebview', {name:this.id});
        return false;
    };

</script>
</html>

from apps.js

Ti.App.addEventListener('fromwebview', function(data) 
{ 
    Titanium.API.info("--> " + data.name);
});

这篇关于使用Titanium的webview引发API事件时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 00:19