Search code examples
javascriptxforms

How can I call JavaScript code from an XForms action?


I'm trying to call a javascript on a button click in XForm. Seems to be an easy task but... I've programmed everything as it is described here and have this added to my xml :

<xforms:trigger>
    <xforms:label>Increment foo with JavaScript</xforms:label>
    <xxforms:script ev:event="DOMActivate">
        alert("Test!")
    </xxforms:script>
</xforms:trigger>

But I get this error wher the page has loaded :

Fatal error: The prefix "ev" for attribute "ev:event" associated with an element type "xxforms:script" is not bound

Did I miss some thing?


Solution

  • This means that the namespace prefix ev is not visible from the <xxforms:script> element.

    As @grtjn mentions in his comment, you have to add the proper XML namespace declaration. For example at the top of your document:

    <xhtml:html
        xmlns:xhtml="http://www.w3.org/1999/xhtml"
        xmlns:ev = "http://www.w3.org/2001/xml-events"
        xmlns:xforms="http://www.w3.org/2002/xforms"
        xmlns:xxforms="http://orbeon.org/oxf/xml/xforms">
        <xhtml:head>
            ...
    

    Note that in recent builds of Orbeon Forms, the prefix on event attributes is now optional, so you can write things like:

    <xxforms:script event="DOMActivate">
    

    But this might not be supported by other implementations.