Re: [Xsltforms-support] Calling Javascript-defined functions from XForms.
Brought to you by:
alain-couthures
|
From: Alain C. <ala...@ag...> - 2015-06-20 08:56:51
|
Hello Cyril,
First, xf:load/@resource can only contains an URL. The protocol (http:,
ftp:, ...), even if it ends with a colon, is not a namespace prefix so
it is not possible to declare its own prefix for Javascript which is
always javascript:. The string after "javascript:" is interpreted as
Javascript instructions: it is not possible to directly call any XPath
function such as instance().
On the contrary, xf:load/xf:resource/@value is always containing an
XPath expression returning a string which has to be a valid value for
xf:load/@resource.
As an extension, XSLTForms allows to directly call Javascript functions
within XPath expressions, without any prefix (because, according to
XForms 1.1 Specifications, only an XPath 1.0 engine is required). This
is why it is possible, with XSLTForms only, to call a Javascript
function for a setvalue action.
Finally, XSLTForms latest builds support, as a convenience, double
quotes and double apostrophes within strings, as specified in XPath2.0+,
instead of having to use " and '.
Please find below the modified test case:
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ev="http://www.w3.org/2001/xml-events">
<head>
<title>Test News</title>
<script type="text/javascript">
function intMyAlert(str) {
alert(str);
return 1;
}
</script>
<xf:model id="mdlMyModel">
<xf:instance id="itcMyInstance">
<data xmlns="">
<text>My Instance Text</text>
<dump/>
</data>
</xf:instance>
</xf:model>
</head>
<body>
<h3>Load Javascript Test</h3>
<div>
<p>Test 1 :
<xf:trigger>
<xf:label>Alert()</xf:label>
<xf:action ev:event="DOMActivate">
<xf:load resource="javascript:alert('Hello
World!')" />
</xf:action>
</xf:trigger>
</p>
<!--
<p>Test 2 :
<xf:trigger>
<xf:label>Alert() + namespace 'js'</xf:label>
<xf:action ev:event="DOMActivate">
<xf:load resource="js:alert('Hello World!')" />
</xf:action>
</xf:trigger>
</p>
-->
<p>Test 3 :
<xf:trigger>
<xf:label>Alert() + resource value</xf:label>
<xf:action ev:event="DOMActivate">
<xf:load>
<xf:resource
value="'javascript:alert(''Hello World!'')'"/>
</xf:load>
</xf:action>
</xf:trigger>
</p>
<p>Test 4 :
<xf:trigger>
<xf:label>Alert() + instance text</xf:label>
<xf:action ev:event="DOMActivate">
<xf:load>
<xf:resource
value="concat('javascript:alert(''',instance('itcMyInstance')/text,''')')"/>
</xf:load>
</xf:action>
</xf:trigger>
</p>
<p/>
<h3>Setvalue Javascript Test</h3>
<xf:trigger>
<xf:label>Function + instance text + setvalue</xf:label>
<xf:action ev:event="DOMActivate">
<xf:setvalue ref="instance('itcMyInstance')/dump"
value="intMyAlert(instance('itcMyInstance')/text)"/>
</xf:action>
</xf:trigger>
</div>
</body>
</html>
What do you think?
--Alain
> Hello,
>
> I'm trying to use the load element with a javascript function, to do
> things like changing the page title or the anchor dynamically, and
> there's some strange problems. I managed to do it with the setvalue
> element though, but I don't know if it's a correct way to do it with
> xforms or if it's just working with Xsltforms.
>
> Here is the code :
>> <?xml version="1.0" encoding="UTF-8"?>
>> <?xml-stylesheet href="xsltforms/xsltforms.xsl" type="text/xsl"?>
>> <html
>> xmlns="http://www.w3.org/1999/xhtml"
>> xmlns:xf="http://www.w3.org/2002/xforms"
>> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xmlns:ev="http://www.w3.org/2001/xml-events"
>> xmlns:js="javascript:code">
>> <head>
>> <title>Test News</title>
>> <script type="text/javascript">
>> function intMyAlert(str) {
>> alert(str);
>> return 1;
>> }
>> </script>
>> <xf:model id="mdlMyModel">
>>
>> <xf:instance id="itcMyInstance">
>> <data xmlns="">
>> <text>My Instance Text</text>
>> <dump/>
>> </data>
>> </xf:instance>
>> </xf:model>
>> </head>
>> <body>
>> <h3>Load Javascript Test</h3>
>> <div>
>> <p>Test 1 :
>> <xf:trigger>
>> <xf:label>Alert()</xf:label>
>> <xf:action ev:event="DOMActivate">
>> <xf:load resource="javascript:alert('Hello
>> World !')" />
>> </xf:action>
>> </xf:trigger>
>> </p>
>>
>> <p>Test 2 :
>> <xf:trigger>
>> <xf:label>Alert() + namespace 'js'</xf:label>
>> <xf:action ev:event="DOMActivate">
>> <xf:load resource="js:alert('Hello World !')" />
>> </xf:action>
>> </xf:trigger>
>> </p>
>>
>> <p>Test 3 :
>> <xf:trigger>
>> <xf:label>Alert() + resource value</xf:label>
>> <xf:action ev:event="DOMActivate">
>> <xf:load>
>> <xf:resource value="js:alert('Hello World
>> !')"/>
>> </xf:load>
>> </xf:action>
>> </xf:trigger>
>> </p>
>>
>> <p>Test 4 :
>> <xf:trigger>
>> <xf:label>Alert() + instance text</xf:label>
>> <xf:action ev:event="DOMActivate">
>> <xf:load
>> resource="javascript:alert(instance('itcMyInstance')/text)" />
>> </xf:action>
>> </xf:trigger>
>> </p>
>>
>> <p/>
>> <h3>Setvalue Javascript Test</h3>
>> <xf:trigger>
>> <xf:label>Function + instance text + setvalue</xf:label>
>> <xf:action ev:event="DOMActivate">
>> <xf:setvalue ref="instance('itcMyInstance')/dump"
>> value="js:intMyAlert(instance('itcMyInstance')/text)"/>
>> </xf:action>
>> </xf:trigger>
>> </div>
>> </body>
>> </html>
> With Test 2 in think the new namespace for javascript is not recognized
> and Test 3 load the script but then tries to load the page « Hello World
> ! ». With Test 4 there's a XSLTForms Exception : « instance is not
> defined ».
>
> Regards,
> Cyril.
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Xsltforms-support mailing list
> Xsl...@li...
> https://lists.sourceforge.net/lists/listinfo/xsltforms-support
|