Project

Profile

Help

Custom events and the window object

Added by Eric van der Vlist almost 4 years ago

In JavaScript custom events are as simple as:

window.addEventListener('AnEvent', function(){alert('AnEvent');}, false);
window.dispatchEvent(new CustomEvent('AnEvent'));

With Saxon CE, I had assumed that you could do something similar:

HTML:

<html>
    <head>
        <title>Hello World for Saxon CE</title>
        <script type="text/javascript" language="javascript" src="saxon/SaxonceDebug/Saxonce.nocache.js"></script>
        <script type="application/xslt+xml" language="xslt2.0" src="repro-event.xsl" data-initial-template="init"></script>
    </head>
    <body>
        <div id="output"> </div>
    </body>
</html>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ixsl="http://saxonica.com/ns/interactiveXSLT" extension-element-prefixes="ixsl" 
    version="2.0">

    <xsl:template name="init">
        <ixsl:set-property name="dummy" 
            select="ixsl:eval(&quot;window.dispatchEvent(new CustomEvent('AnEvent'))&quot;)"/>
    </xsl:template>

    <xsl:template match="ixsl:window()" mode="ixsl:onAnEvent">
        <xsl:result-document href="#output">
            <p>Got it!</p>
        </xsl:result-document>
    </xsl:template>

</xsl:stylesheet>

I have tested this with Firefox and Chromium and the second template is not activated in both cases.

Using JS developer tools I can see that an event handler has been installed:

window.onAnEvent
function (){try{var b;var c=arguments,d;var e;0!=s3&&(e=j3(),2E3<e-u3&&(u3=e,t3=zba()));if(0==s3++){var f=(v3(),w3),g,n;if(f.b){n=null;do g=f.b,f.b=null,n=y3(g,n);while(f.b);f.b=n}d=!0}else d=!1;try{if(l3)try{b=a.apply(this,c)}catch(o){if(o=v(o),z(o,342))z3(o);else throw o;}else b=a.apply(this,c)}finally{if(c=d){var s=(v3(),w3),u,x;if(s.c){x=null;do u=s.c,s.c=null,x=y3(u,x);while(s.c);s.c=x}}--s3;c&&-1!=t3&&($wnd.clearTimeout(t3),t3=-1)}return b}catch(w){throw w;}}

But manually running "window.dispatchEvent(new CustomEvent('AnEvent'))" returns true and does not run the template.

Manually running "window.onAnEvent(new CustomEvent('AnEvent'))" does execute the template!

Have I missed something or is there something wrong with the way the handler is registered?

Thanks,

Eric


Please register to reply