2

I have a peculiar problem with having a JavaScript for loop in XSL document. Here it goes:

I am calling a JavaScript function on the click of a CheckBox. Following is what I wanted to do in the javascript function ::

function SeelctAll()
{
     for(var cnt = 0; cnt < 100; cnt++)
     {
          //Business Logic here.
     }
}

For this I replaced < with &lt; and tried. I got an error saying "Object Expected". I then enclosed the whole function inside a <![CDATA[ section and tried. Still I got the same "Obejct Expected" error.

Any help on this would be greatly appreciable.

1
  • I think we're going to need more details about the problem. Can you provide the relevant part of the XSL document? What's inside the // Business Logic here part? That seems to be where the problem originates, so that would be relevant as well. Commented Apr 13, 2010 at 15:04

3 Answers 3

1

We'd need to see a bit more of the code here, and what process you're using to convert JavaScript in an XSL document to JavaScript in an HTML page for execution. Whilst &lt; escaping and a CDATA section are both valid ways to include out-of-band characters in an XML file, when you get to the browser side you're probably handling the page as HTML rather than native-XML, at which point the rules are different, and care is required with your HTML-generation to ensure that the output from the XSL transform is acceptable to browsers.

See if it really is the < causing the trouble rather than the elided ‘Business Logic’, by avoiding it completely. eg. replace it with something like 100>cnt.

(In any case, in general you want to keep scripting logic out of the body of an HTML page. It's better off in an external script, where you don't have to worry about the rules for embedding it in another markup language.)

Sign up to request clarification or add additional context in comments.

Comments

0

Works for me:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="html" encoding="utf-8" indent="yes"/>

  <xsl:template match="/">
    <html>
      <head>
        <script type="text/javascript"><![CDATA[
          function SelectAll() {
            var x = 0;
            for(var cnt=0; cnt<100; cnt++) {
              x = cnt; // whatever
            }
            alert(x);
          }            
          ]]>
        </script>
      </head>
      <body>
        <div onclick="SelectAll()">Click!</div>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

This generates:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type="text/javascript">
    function SelectAll() {
      var x = 0;
      for(var cnt=0; cnt<100; cnt++) {
        x = cnt; // whatever
      }
      alert(x);
    }
  </script>
</head>
<body>
  <div onclick="SelectAll()">Click!</div>
</body>
</html>

and it alerts the expected "99".

3 Comments

I am just having an alert statement inside the loop. BTW I have multiple xsl:templates and I do not have the <html><head><body> tags. I directly have a <table> tag.
@Nagendra: And... what does that mean for this answer? Your's does not work, this does. Spot the differences, draw a conclusion.
Thanks for the reply. I have found the differences and replaced the <xsl:output tag in my code with the one present in your code and everything started working.
0

I believe the problem is much simpler. You named the function SeelctAll(). I think you are calling the wrong function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.