Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • D-au 26 posts 76 karma points
    Dec 09, 2010 @ 05:06
    D-au
    0

    javascript "for" function not working?

    Hi Guys, just a quick question. I notice that the javascript 'for' function is not working when it is put to use in xslt file. I'm trying to create a simple gallery using Jquery fade in features. My script code is looking somewhat like this:

    <!--Repetitive JavaScript-->
    <script>
    {
          $("#imgjq1").click(function () {
        $("#imgreal2").hide();
        $("#imgreal3").hide();
        $("#imgreal4").hide();
        $("#imgreal5").hide();
        $("#imgreal6").hide();
        $("#imgreal7").hide();
        $("#imgreal8").hide();
        $("#imgreal9").hide();
        $("#Dum").hide();
              $("#imgreal1").fadeIn(300, function () {
              });
              return false;
            });
    }
    
    {
          $("#imgjq2").click(function () {
        $("#imgreal1").hide();
        $("#imgreal3").hide();
        $("#imgreal4").hide();
        $("#imgreal5").hide();
        $("#imgreal6").hide();
        $("#imgreal7").hide();
        $("#imgreal8").hide();
        $("#imgreal9").hide();
        $("#Dum").hide();
              $("#imgreal2").fadeIn(300, function () {
              });
              return false;
            });
    }
    <!--- And So on.....---->

    Which I guess should be able to be simplified by using the javascript 'for' function:

    But even a simple javascript code as:

    <script type="text/javascript">
    var i=0;
    for (i=0;i<=5;i++)
    {
    document.write("The number is " + i);
    document.write("<br />");
    }
    </script>

    Gives xslt an error, while putting it in the master template doesn't make it work either.

    Any intake on this issue or even a better approach to do this? 

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Dec 09, 2010 @ 10:02
    Michael Latouche
    0

    Hi,

    Just a wild guess here, but did you try by specifying the script language? so:

    <script language="JavaScript" type="text/javascript">

    Cheers,

    Michael.

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Dec 09, 2010 @ 10:17
    Chriztian Steinmeier
    0

    Hi D-au,

    This has to do with escaping the < characters in JavaScript- you can wrap your script content in a CDATA Section to fix this, e.g.:

    <script type="text/javascript"><![CDATA[
    var i=0;
    for (i=0;i<=5;i++)
    {
    document.write("The number is " + i);
    document.write("<br />");
    }]]>
    </script>

    But if you don't need to dynamically write any of the JavaScript, it'd be much better to have it in a separate file and include it with a standard <script src="..."> element...

    /Chriztian

  • Harm-Jan Abbing 62 posts 102 karma points
    Dec 09, 2010 @ 11:49
    Harm-Jan Abbing
    0

    And with arrays you can use "for(var item in array)". That way you don't need to use the less or greater than signs.

Please Sign in or register to post replies

Write your reply to:

Draft