Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Turn a javascript into a javascript function?

    Hi you all,

    I have some javascript that I have written into the <body> section and it works great. But I would like to make it into a javascript function and define the function in the <head> section. Then in the <body> section write a small bit of javascript that would call the function() object.

    Here is section of the code from the htm file as it is now and works as intented.

    Code:
    <div align="center">
      <center>
      <table border="0" cellpadding="5" cellspacing="0" width="90%">
        
    <SCRIPT LANGUAGE="JavaScript">
    
      var SizeH = '0'; // for height of tumbnail image
      var SizeW = '0';// for width of tumbnail image
      var nR = '4';     // for number of rows in table
      var nC = '5';     // for number of cells per row in table
      var nS = '0';     // for count of slides used in table to index an array of image.src
    
      for(row=0; row<=nR-1; row++)
      {
          document.write("<tr>");	
          for(cell=0; cell<=nC-1; cell++)
          {
            nS = cell + (row * nC)
            if(arrSize[cell + (nS)] == true)
              {
                SizeH = '67';
                SizeW = '100';
               }
             else{
                SizeH = '100';
                SizeW = '67';
               }
    
            document.write("<td width='20%' height='116' valign='middle' align='center'>");
            document.write('<a href="javascript:winopen(\'../single_Slide.htm\', &quot;' + arrSlidePaths[nS] + '&quot;)">');
            document.write("<img border='1' src='images/Other/blank.gif' alt = 'Click here'");
            document.write("width=" + SizeW + "height=" + SizeH + "></a></td>");
            
            
            
          }
          document.write("</tr>");
          document.write("<tr>");
          for(cell=0; cell<=nC-1; cell++)
          {
            nS = cell + (row * nC)
            document.write("<td width='20%' height='20' valign='bottom' align='center'><font face='Comic Sans MS' size='4' color='#CC3300'>" + arrSlideNumbers[nS] + "</font></td>");
          }  
          document.write("</tr>");
    	}
    </script>
    
      </table>
      </center>
    Now I think if I put all of the above code that is between the <script></script> tags inside a pair of {} brackets and then add this bit of code before the { opening bracket.

    function writeCells(NumR, NumC)

    Then change:

    var nR = '4';
    var nc = '5':

    to:

    var nr = NumR;
    var nc = NumC;

    I will have correctly define the function, right? And I can move this to the <head> section?

    Now in the <body> section I should be able to insert this where I want the rows and cells of the table create.

    <div align="center">
    <center>
    <table border="0" cellpadding="5" cellspacing="0" width="90%">


    <SCRIPT LANGUAGE="JavaScript">
    writeCells('4', '5');
    </script>


    </table>
    </center>
    </div>


    Is this correct?

    I don't know what I'm doing wrong, but I can't seem to get this to work.

    PhotoJoe
    Last edited by PhotoJoe47; 10-21-2005 at 12:13 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Code:
    <script type="text/javascript">
    
    function WriteCells (nR,nC) {
      var SizeH = 0; // for height of tumbnail image
      var SizeW = 0;// for width of tumbnail image
      var nS = 0; // for count of slides used in table to index an array of image.src
    
      for(row=0; row<=nR-1; row++)
      {
          document.write("<tr>");	
          for(cell=0; cell<=nC-1; cell++)
          {
            nS = cell + (row * nC)
            if(arrSize[cell + (nS)] == true)
              {
                SizeH = '67';
                SizeW = '100';
               }
             else{
                SizeH = '100';
                SizeW = '67';
               }
    
            document.write("<td width='20%' height='116' valign='middle' align='center'>");
            document.write('<a href="javascript:winopen(\'../single_Slide.htm\', &quot;' + arrSlidePaths[nS] + '&quot;)">');
            document.write("<img border='1' src='images/Other/blank.gif' alt = 'Click here'");
            document.write("width=" + SizeW + "height=" + SizeH + "></a></td>");
            
            
            
          }
          document.write("</tr>");
          document.write("<tr>");
          for(cell=0; cell<=nC-1; cell++)
          {
            nS = cell + (row * nC)
            document.write("<td width='20%' height='20' valign='bottom' align='center'><font face='Comic Sans MS' size='4' color='#CC3300'>" + arrSlideNumbers[nS] + "</font></td>");
          }  
          document.write("</tr>");
         }
    }
    </script>
    Then call it like:
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    writeCells(4, 5);
    </script>
    You are passing the values in as strings, but try to use them in for-loops as integers. So i removed the quotes in the function call. Als you dont have to define extra variables to use your function arguments in the function body.

    Try it, i have not tested it, but i believe it should work
    I am the luckiest man in the world

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Arizona
    Posts
    336
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Roelf,

    I copy and pasted you code into my page. At first it didn't work I got an error of "Object expected". I have seen this a few times and looked at the function name both at where it was defined and call. It was defined WriteCells() and called writeCells(). So I change it in the define section to writeCells(). Then I notice an old bug that I had fixed already before. Some of the tumbnails were the wrong size. I had copy and pasted from the wrong version of the page I was writing. So that was easy to fix.

    I just needed to change this line:

    if(arrSize[cell + (nS)] == true)

    to this:

    if(arrSize[nS] == true)

    Now it is working great.

    So this means I can now add the cellWrite() function to my tumbnail.js file.

    Thanks again,
    PhotoJoe


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •