title: javascript code in XSLT
body: <p>I'm using XSLT to transform an XML into HTML on the client side (Chrome browser).</p>
  
  <p>I'm trying to add <code>&lt;script&gt;</code> HTML tag to the XSLT but it seems that the code in it is never evaluated on the generated HTML, although I've specified <code>defer</code>.<br>
  On the other hand, <code>onclick</code> event itself runs OK.</p>
  
  <p>Here is an example of the XSLT which demonstrates the issue:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
  &lt;xsl:output method="html"
              encoding="UTF-8"
              indent="no"/&gt;
  
  &lt;xsl:template match="/"&gt;
  &lt;html&gt;
  &lt;head&gt;
  
  &lt;script type="text/javascript" defer="defer"&gt;
  &lt;xsl:text&gt;
  &lt;![CDATA[
  function test(){
    window.alert('Test');
  }
   ]]&gt;
   &lt;/xsl:text&gt;
  &lt;/script&gt;
  
  
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;button onclick="window.alert('Test')"&gt;This works&lt;/button&gt;      
    &lt;br/&gt;
    &lt;button onclick="test()"&gt;This does not work&lt;/button&gt;      
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The XML file does not matter is this example. You can try the above example on <a href="http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&amp;xsltfile=cdcatalog" rel="nofollow">W3Schools online XSLT transformation</a>  </p>
  
  <p>In this example, clicking on <code>This does not work</code> yields an error: <code>Uncaught ReferenceError: test is not defined</code>.  </p>
  
  <p>What am I doing wrong here?</p>
  
  <h2>Update</h2>
  
  <p>The problem only happens when I'm performing the XSLT transformation itself in javascript. Here is the piece of code that is doing that in my case:</p>
  
  <pre class="lang-js prettyprint-override"><code>var processor = new XSLTProcessor(),
    htmlResult;
  
  processor.importStylesheet(xsl);
  htmlResult = processor.transformToFragment(xhr.responseXML, document);
  document.getElementById("result").appendChild(htmlResult);
  </code></pre>
  
  <h2>Update</h2>
  
  <p>I also need the following to work correctly when they appear in the XSLT file:</p>
  
  <ul>
  <li>Loading external javascript files using <code>script</code> element:<br>
  <code>&lt;script type="text/javascript" src="/somelibrary.js" /&gt;</code> </li>
  <li>Bare <code>&lt;script&gt;</code> elements with javascript code in them, that call functions which are declared in an external javascript file, loaded by an earlier <code>script</code> element.</li>
  </ul>