locked
XSLT generated JavaScript not executed in IE RRS feed

  • Question

  • My problem is the following:

    I generate JavaScript Code in XSLT that should be excecuted in the IE (V. 6.0). I create a <select> box with some <option> in it. Instead of using <xsl:choose><xsl:when> repeatly I thought I just create a Function that get's called afterwards and sets the right option.

    The XML:
    <root>
    <element>2</element>
    <element>1</element>
    </root>


    The XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output version="1.0" encoding="utf-8" omit-xml-declaration="yes" media-type="text/html" />
    <!-- main template -->
    <xsl:template match="/">
    <h2>This is the transformation</h2>
    <script type="text/javascript">
    function evalSelectedOption(objSel, optnValue) {
    if(objSel != null) {
    for(var i=0;i!=objSel.length;i++) {
    if(objSel.options[\i].value == optnValue)
    objSel.options[\i].selected=1;
    }
    }
    alert(objSel.id);
    alert("option with value of  "+optnValue+" set to 'selected'");
    }
    </script>
    <xsl:choose>
    <xsl:when test="/Root/element">
    <xsl:for-each select="/Root/element">
    <xsl:call-template name="templName" />
    </xsl:for-each>
    </xsl:when>
    </xsl:choose>
    </xsl:template>
    <!-- template that gets called -->
    <xsl:template name="templName">
    <xsl:param name="id" select="generate-id()" />
    <select id="{$id}" >
    <option value="1">first option</option>
    <option value="2">second option</option>
    <option value="3">third option</option>
    </select>
    <script type="text/javascript">
    evalSelectedOption(document.getElementById('<xsl:value-of select="$id"/>'),'<xsl:value-of select="."/>');
    </script>
    </xsl:template>
    </xsl:stylesheet>



    Now if you put the code in seperate files and link them together, for example using <?xml-stylesheet type="text/xsl" href="nameOf.xsl" ?> in the .xml and call it in IE it works.

    But I wan't to use Data-Islands since the XML and XSLT are generated serverside. So this is just an example of the concept I want to use.

    Now if I load the documents in the DOM in JavaScript and do the transformation, the JavaScript that is generated by the XSLT-Processor won't be executed anymore.

    Is this a bug or a feature? *lol* Or maybe am I missusing something? Its working in Firefox.

    I can post the JavaScript also if it would help!

    Greetins
    Spanky

    EDIT: The example works, you just need to cut the backslashes before the i in ..options[\i].. There was a light displayed leaving it as is.
    Monday, September 18, 2006 8:04 AM

Answers

  •  Spankmaster79 wrote:


    I generate JavaScript Code in XSLT that should be excecuted in the IE (V. 6.0).
    Now if you put the code in seperate files and link them together, for example using <?xml-stylesheet type="text/xsl" href="nameOf.xsl" ?> in the .xml and call it in IE it works.

    But I wan't to use Data-Islands since the XML and XSLT are generated serverside. So this is just an example of the concept I want to use.

    Now if I load the documents in the DOM in JavaScript and do the transformation, the JavaScript that is generated by the XSLT-Processor won't be executed anymore.

    I can post the JavaScript also if it would help!

    If you load an XML and XSLT document "in the DOM in JavaScript and do the transformation" then with MSXML which IE uses you either get a string result or a DOM document result. In both cases up to that point no script in the transformation result at all will be executed at all as script in the result is not executed at all by the XSLT processor, unless the result is handled by some application then understands the result and knows how to execute script in the result, in your case an HTML document or a HTML snippet.

    So it is crucial that you show us how you use the transformation result and add/insert it into some HTML document rendered in IE as only then IE handles the HTML and the script.

    Inserting HTML snippets with script in a HTML document with IE to have the script executed is tricky, if you use innerHTML and/or insertAdjacentHTML then take note that IE documents that you should use the defer attribute (e.g. <script type="text/javascript" defer> in HTML respectively as you author that stuff in an XML document, your stylesheet e.g. <script type="text/javascript" defer="defer">).

    Monday, September 18, 2006 12:35 PM

All replies

  •  Spankmaster79 wrote:


    I generate JavaScript Code in XSLT that should be excecuted in the IE (V. 6.0).
    Now if you put the code in seperate files and link them together, for example using <?xml-stylesheet type="text/xsl" href="nameOf.xsl" ?> in the .xml and call it in IE it works.

    But I wan't to use Data-Islands since the XML and XSLT are generated serverside. So this is just an example of the concept I want to use.

    Now if I load the documents in the DOM in JavaScript and do the transformation, the JavaScript that is generated by the XSLT-Processor won't be executed anymore.

    I can post the JavaScript also if it would help!

    If you load an XML and XSLT document "in the DOM in JavaScript and do the transformation" then with MSXML which IE uses you either get a string result or a DOM document result. In both cases up to that point no script in the transformation result at all will be executed at all as script in the result is not executed at all by the XSLT processor, unless the result is handled by some application then understands the result and knows how to execute script in the result, in your case an HTML document or a HTML snippet.

    So it is crucial that you show us how you use the transformation result and add/insert it into some HTML document rendered in IE as only then IE handles the HTML and the script.

    Inserting HTML snippets with script in a HTML document with IE to have the script executed is tricky, if you use innerHTML and/or insertAdjacentHTML then take note that IE documents that you should use the defer attribute (e.g. <script type="text/javascript" defer> in HTML respectively as you author that stuff in an XML document, your stylesheet e.g. <script type="text/javascript" defer="defer">).

    Monday, September 18, 2006 12:35 PM
  •  Martin Honnen wrote:
    ...tricky, if you use innerHTML and/or nsertAdjacentHTML then take note that IE documents that you should use the defer attribute


    That helped a bit. The script gets executed now. I didn't know defer was needed. I still do have the problem that it only gets executed once. But I'll try to fix it.

    Thx
    Spanky

    EDIT: Are there any other attributes that control when a <script> element get's parsed as JavaScript or as String? Because this doesn't always work.
    Tuesday, September 19, 2006 11:41 AM