A more generic solution using the event JavaScriptError in your session Instance:
For example:
Sub JavaScriptError(ErrorName as String, ErrorMessage as String, ErrorStack as String) Handles JavaScriptError
if ErrorName = "json-data" and ErrorStack.Middle(0,1) = "{" then
dim data as new jsonItem(ErrorStack)
//do something with data
System.DebugLog "name=" + data.value("name")
end if
End Sub
In javascript you can use this helper function:
function sendDataToXojo(data){
o = new XojoWeb.JSONItem(); o.set("message", "json-data"); o.set("name", "json-data"); o.set("stack", JSON.stringify(data));
console.log(o);
XojoWeb.session.comm.triggerServerEvent("Event","JavaScriptError", o);
}
You can use you App (web application) html header property to inject something like this:
<script>
var _loadCallbacks = [];
var a = setInterval(function(){
if(!window.XojoWeb) return;
clearInterval(a);
for (var i = 0, l = _loadCallbacks.length; i < l; i++) { _loadCallbacks[i].apply(window); }
}, 25);
function xojoReady(fn){
_loadCallbacks.push(fn);
}
//Your code here
//xojoReady(fn) is similar to jQ Ready but it is executed when XojoWeb is loaded.
xojoReady(function(){
XojoWeb.sendDataToXojo = function(data){
o = new XojoWeb.JSONItem(); o.set("message", "json-data"); o.set("name", "json-data"); o.set("stack", JSON.stringify(data));
XojoWeb.session.comm.triggerServerEvent("Event","JavaScriptError", o);
}
});
</script>
This script will do two things it will add the following functions:
XojoWeb.sendDataToXojo(dataObj)
and a XojoReady(fn)
XojoWeb.sendDataToXojo
you can use from anywhere that can access XojoWeb. For example:
var data = { name: "Jose", salary: 200 };
XojoWeb.sendDataToXojo(data);
The XojoReady()
is just a little helper function to execute callbacks when XojoWeb is available.
The htmlViewer is a bit of different story if you don’t have a reference to XojoWeb but I haven’t used the htmlViewer to see how is currently implemented.
For iframe the common trick to talk to the parent is to use window.parent.postMessage()
and window.addEventListener("message")
in your main page to handle the message.
Hope it helps…