sparkyspider
sparkyspider

Reputation: 13529

Cannot use document.write in an XSLT stylesheet (Firefox)

I've created an XSL stylesheet that has some embedded JavaScript. It calls functions that in turn call document.write() to print the results of these functions to the page.

<xsl:foreach...>
<div class="directionBlock">
<script type="text/javascript">writeDirection('<xsl:value-of select="Direction"/>');</script>
</div>
</xsl:foreach...>

Unfortunately, in firefox, document.write() is not supported. What to do?

Upvotes: 0

Views: 1562

Answers (2)

sparkyspider
sparkyspider

Reputation: 13529

The way I implemented a solution is to call a function and pass the row number:

<xsl:foreach...>
  ...
  <div class="directionBlock">
    <script>myJsFunction('<xsl:value-of select="Direction"/>', <xsl:value-of select="position()"/>);</script>
  </div>
  ...
</xsl:foreach...>

This in turn used JQuery to access the element in question, using nth-child(position) to get the row, find a selector and use .html() to insert code into that element.

Thanks for all your help!

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167696

Why do you need both XSLT and Javascript to output HTML? If your aim is to output something in the div containing the script element then consider to do it with XSLT alone. If you really need to use Javascript to create contents then consider to use methods like createElement and appendChild instead of document.write. That is as far as a general answer can help, if you need specific help then we need to see details of your code like that writeDirection function and the argument you pass to it.

To give you some outline of sample code, if you writeDirection function needs to add content to the div then put an id on the div e.g. <div id="db1" class="directionBlock">...</div>, then pass it to the writeDirection function e.g. <script type="text/javascript">writeDirection('<xsl:value-of select="Direction"/>', document.getElementById('db1'));</script>, then in that function simply do

function writeDirection(dir, elementToWriteTo) {
  // instead of document.write(stuff) use
  var span = document.createElement('span');
  span.innerHTML = stuff;
  elementToWriteTo.appendChild(span);
}

Upvotes: 1

Related Questions