JavaScript to loop in an XSL document
I have a peculiar problem with having a JavaScript loop for an XSL document. Here it is:
I am calling a JavaScript function when a CheckBox is clicked. The 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 <
and tried. I got the error "Object expected". Then I turned on the whole function inside the section <![CDATA[
and tried. However, I got the same "Obejct Expected" error.
Any help on this point would be greatly appreciated.
a source to share
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 warns of the expected "99".
a source to share
We need to see a little more code here and what process you are using to transform JavaScript to XSL document in JavaScript in an HTML page for execution. While <
escaping and the CDATA section are valid ways of including out-of-band characters in an XML file, when you get to the browser side, you are probably treating the page as HTML, not native-XML, which has different rules, and care is required to generate HTML. to ensure that the exit from the XSL transformation is valid for browsers.
See if this is actually <
causing the problem and not fixed "Business Logic" by avoiding it entirely. eg. replace it with something like 100>cnt
.
(Anyway, in general, you want to keep the scripting logic out of the body of the HTML page. Better to use an external script where you don't have to worry about the rules for embedding it in another markup language.)
a source to share