简体   繁体   中英

Pushlets working with HTML5 canvas or Javascript

Does anyone have experience with Pushlets?

I have been working on it several days. I can make it work on regular javascript, but when I add HTML canvas and use javascript to draw something based on the "push"ed data, it doesn't work.

In my simple example:

document.getElementById('sometag').innerHTML = event.get("x");
document.getElementById('sometag').innerHTML = event.get("x");
...
document.getElementById('sometag').innerHTML = event.get("x");

if I keep all these regular tag there is no problem, but when I add:

document.getElementById('canvas').getContext('2d').fillRect(....);

it doesn't work. The error says can not receive XML data.

So any help? Thanks in advance.

You probably need to evaluate scripts in HTML pushed from server. Something like this:

function extractScripts(html) {
// based on PrototypeJs
    var ScriptFragment = "<script[^>]*>([\\S\\s]*?)<\/script>";
    var matchAll = new RegExp(ScriptFragment, "img");
    var matchOne = new RegExp(ScriptFragment, "im");
    return (html.match(matchAll) || []).map(function(scriptTag) {
      return (scriptTag.match(matchOne) || ['', ''])[1];
    });
}
function evalScripts(html) {
    return extractScripts(html).map(function(script) { return eval(script) });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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