JavaScript For Loop in XSL document
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 开发者_StackOverflow社区::
function SeelctAll()
{
for(var cnt = 0; cnt < 100; cnt++)
{
//Business Logic here.
}
}
For this I replaced <
with <
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.
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 <
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.)
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".
I believe the problem is much simpler. You named the function SeelctAll()
. I think you are calling the wrong function.
精彩评论