SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problem with my code....

    Hello,

    i am having a slight problem with my code working properly. here is the problem.... is there a way in javascript to check if a condition is true or false using an array. example: i have 3 external .js files that are called from the main .htm file. these .js files hold a "case" statement and the path is directed to the specific file according to the "switch" statement. ie:

    whichcalendar.js -
    switch (whichcalendar)
    {
    case "all" : loads calendarall.js; break;
    case "prt" : loads calendarpart.js; break;
    }

    calendarpart.js -
    switch (year)
    {
    case 1970 : loads calendar19701979.js; break;
    }

    calendar19701979.js -
    switch (year)
    { case 1970 : switch (month)
    { case 1 : var nopanels = new array(1, 2, 3, 4, 11, 17, 18, 19, 25, 29, 31); break;
    }

    calendar19701979.js is called from with calendarpart.js and calendarpart.js is called from within whichcalendar.js which is loaded from the .htm document.

    how do i access "nopanels()" in an IF, WHILE, etc... statement that will properly display the dates listed without a link attached to them.

    example: i want the days of the month to have a "link" attached to them except the dates listed in no panels.

    i have tried: if (i != nopanels(i)).... it cause the error message: "nopanels - is not defined" and nopanels is specified before the "if" statement is used....

    there may be a lot of files, this is cause i am trying to keep the files small, instead of 1 huge file. hope this makes since.

    thanks for your help....
    John....

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    //html
    <a id="link_1" href="dateInfo_1.html">1</a>
    <a id="link_2" href="dateInfo_2.html">2</a>
    <a id="link_3" href="dateInfo_3.html">3</a>
    <a id="link_4" href="dateInfo_4.html">4</a>
    
    //js
    for (var i = 0, l = nopanels.length; i < l; i++) {
      var a = document.getElementById("link_" + nopanels[i]);
      a.href = "#"; // quick and dirty
      a.onclick = "return false"; // or whatever
    
      // or some DOM manipulation to replace the anchor with plain text
    }
    Notice array elements are accessed with square brackets, and nopanels needs to be declared outside of any other nested functions.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    below is the code that i am trying to get to work, it is a snippet:

    if (i != nopanels[i])
    {
    document.write("<a target='_parent' href='../loveisdisppanel.htm?" + "loveis" + panelyear + panelmonth + padout(i) + ".jpg" + "&" + panelyear + "&" + panelmonth + "&" + padout(i) + "&" + paneldir + "/" + "'>");
    document.write('<font color="gold" size="3" face="comic sans MS"><b><i>');
    document.write(padout(i));
    }
    else
    { // future days in month
    document.write('<font color="#00ff00" size="3" face="comic sans MS"><b><i>');
    document.write(padout(i));

    what it is supposed to do is if there is a panel for which i do not have as referenced in the external .js file of "switch (panelyear)
    {
    case 1970 : switch (panelmonth)
    {
    case 1 : var nopanels = new array(1, 2, 3, 4, 11, 17, 18, 19, 25, 29, 31); break; }" then it is supposed to do the "else" part of the "if" statement which does not have any links assigned to it, otherwise it is supposed to add the link to the day number in the calendar.

    john....

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think there's a built in function to check if an item is in an array. However, in your case it's easy enough to convert the array to a string and use indexOf:
    Code:
    var nopanelStr = "." + nopanels.join(".") + ".";
    
    // will give string e.g. ".1.2.3.4.11.17.18.19.25.29.31."
    
    if (nopanelStr.indexOf("." + i + ".") != -1) {
      // i matches array value so no link
    }
    else {
      // include link
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    below is the link to calendardisppart.js file that will display the calendars for the years that i have no panels for.

    http:/johnandcheryl.0catch.com/loveis/js/scripts/calendardisppart.js

    http:/johnandcheryl.0catch.com/loveis/loveishtm/loveisdispyears.htm?1970&04&19701979&prt

    loveisdispyears.htm is the calling file that calls the external file:
    loveiswhichcalendar.js which calls calendarall.js or calendarpart.js

    calendarall.js works fine no problems
    calendarpart.js calls calendarpart19701979.js
    calendarpart19701979.js calls calendardisppart.js
    calendardispart.js displays the calendar for the months that i do not have all the panels. with this i am getting the following errors:

    'names' undefined - line 169, char 1
    object expected - line 138 char 27

    this is so frustrating, i am so close to this working and there is probably something that i am missing or overlooking....

    thanks,
    john....

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yikes!
    It's probably best to try and simplify all that code first.
    You don't need the calendarpart.js file. How about a function instead?
    Code:
    var stub = '<scr' + 'ipt language="javascript" src="http:\/\/johnandcheryl.0catch.com\/loveis\/js\/scripts\/calendarpart';
    var decade = Math.floor(panelyear / 10) * 10;
    var yrString = "" + decade + (decade + 9);
    document.write(stub + yrString + '.js"><\/script>');
    That replaces the whole of the calendarpart.js file and is now small enough to include on the main page in an if statement.

    Also, move the loveiswhichcalendar.js file onto the main page as well. There's no need for a call back to the server for such a small script.

    Simplifying the code will make it much easier to spot where problems are popping up and will make it much easier to understand and modify the code in future.

    If you have a go at squashing it down, let me know and I'll have another look.

  7. #7
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All that switching isn't needed either. JavaScript will let you use arbitrary keys/indexes for objects/arrays. You can set up an array to hold all the nopanels info.
    Code:
    var nopanels = []; // Array literal quicker/easier than 'new array()'
    nopanels[1970] = [];
    nopanels[1970][1] = [1, 2, 3, 4, 11, 17, 18, 19, 25, 29, 31];
    nopanels[1970][2] = [3, 7, 8, 14, 17, 18, 19, 20, 21, 22, 25, 28];
    nopanels[1970][3] = [1, 5, 6, 7, 8, 9, 14, 22, 26, 27, 29];
    // etc.
    Then, when it's time to use the data, assign it to an easier to use variable.
    Code:
    var monthData = nopanels[1970][2];

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i agree your sugestions are very helpful and i am using them as you have suggested, here is what i came up with from your earlier post.... instead of a "if" statement i used a "switch" statement. the calendar for all panels loads with no errors however it does not display the calendar. the calendar for the partial panels still displays errors, it says that 'names' is not defined but is called just like the one in calendardispall.js is called.. is the syntax and format correct to work properly. i havent tried your latest suggestion with the nopanels yet....

    <script LANGUAGE="JavaScript">
    switch (whichcalendar)
    {
    case "all" : {
    var dispall = '<scr' + 'ipt language="javascript" src="http:\/\/johnandcheryl.0catch.com\/loveis\/js\/scripts\/calendardispall';
    document.write(dispall + '.js"><\/script>');
    // displays calendar with links for all days in specified month
    calendarall((panelmonth - 1), panelyear);
    break;
    }
    case "prt" : {
    var stub = '<scr' + 'ipt language="javascript" src="http:\/\/johnandcheryl.0catch.com\/loveis\/js\/scripts\/calendarpart';
    var decade = Math.floor(panelyear / 10) * 10;
    var yrString = "" + decade + (decade + 9);
    document.write(stub + yrString + '.js"><\/script>');
    // display calendar with no links for non panels for specified month
    var disppart = '<scr' + 'ipt language="javascript" src="http:\/\/johnandcheryl.0catch.com\/loveis\/js\/scripts\/calendardisppart';
    document.write(disppart + '.js"><\/script>');
    calendarpart((panelmonth - 1), panelyear);
    break;
    }
    }
    </script>

  9. #9
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In calendardisppart.js you have a number of if...else blocks. The if in the main else block is missing a closing brace. Actually check all the braces carefully. Put them round the else block as well.
    Code:
     else // not current day
    // missing { ?
         if (column || 0)
         {
            if {}
            else {}
         // missing } ?
        else
       {
       }
    // missing } ?
    And what does if (column || 0) do? Be careful, because 0 is false in js

    In the code for 1970, the array for month 12 has a trailing comma. Probably best to lose that.

  10. #10
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is what i did, there was a "}" missing and i fixed it.... i also made 2 separate .htm files, loveisdispyearsall.htm and loveisdispyearspart.htm. the loveisdispyearsall.htm file is working fine with no problems. the loveisdispyearspart.htm displays the calendar for the selected month, however the dates that are missing are also showing a link nd there not supposed to. i placed the following in the loveisdispyears.htm file:

    column = 0; column is a counter for the number to columns in the table before it is reset to zero....

    <script LANGUAGE="JavaScript">
    switch (panelyear)
    {
    case 1970 : switch (parseInt(panelmonth))
    {
    case 1 : var nopanels = new array(1, 2, 3, 4, 11, 17, 18, 19, 25, 29, 31); break;
    case 2 : var nopanels = new array(3, 7, 8, 14, 17, 18, 19, 20, 21, 22, 25, 28); break;
    case 3 : var nopanels = new array(1, 5, 6, 7, 8, 9, 14, 22, 26, 27, 29); break;
    case 4 : var nopanels = new array(1, 3, 4, 5, 8, 12, 18); break;
    case 5 : var nopanels = new array(3, 7, 10, 16, 21, 23, 25, 26, 29, 30, 31); break;
    case 6 : var nopanels = new array(4, 7, 11, 12, 13, 16, 17, 19, 20, 21, 22, 24, 26, 27, 28, 29, 30); break;
    case 7 : var nopanels = new array(1, 2, 3, 4, 8, 11, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31); break;
    case 8 : var nopanels = new array(1, 2, 5, 6, 8, 12, 14, 15, 19, 20, 22, 24, 27, 29, 31); break;
    case 9 : var nopanels = new array(2, 4, 5, 6, 7, 13, 16, 17, 20, 21, 22, 27, 30); break;
    case 10 : var nopanels = new array(3, 4, 11, 17, 18, 20, 24, 25, 30); break;
    case 11 : var nopanels = new array(1, 3, 5, 6, 8, 15, 17, 18, 19, 20, 22, 24, 25, 29); break;
    case 12 : var nopanels = new array(6, 8, 10, 11, 12, 13, 15, 16, 18, 19, 20, 21, 27); break;
    }
    }
    </script>

    just below the <body> statement and i tried it in the <head> section also and it still displays all the dates with link even the ones that are specified above. and i have changed the following in the calendar function:

    else // not current day
    {
    if (column || 0)
    { // past days in month
    document.write('<td ' + tdcol + '>');
    var nopanelStr = "." + nopanels.join(".") + "."; * is this in the right spot *
    if (nopanelStr.indexOf("." + i + ".") != -1)
    { // no panels for month - not linked
    document.write('<font color="#00ff00" size="3" face="comic sans MS"><b><i>');
    document.write(padout(i));
    document.write('</font></b></i>');
    }
    else
    { // existing panels for month - panels linked
    document.write("<a target='_parent' href='../loveisdisppanel.htm?" + "loveis" + panelyear + panelmonth + padout(i) + ".jpg" + "&" + panelyear + "&" + panelmonth + "&" + padout(i) + "&" + paneldir + "/" + "'>");
    document.write('<font color="gold" size="3" face="comic sans MS"><b><i>');
    document.write(padout(i));
    document.write('</a></font></b></i>');
    }
    document.write('</td>');
    }
    else
    { // Sundays in month
    document.write('<td ' + tdcol + '>');
    document.write("<a target='_parent' href='../loveisdisppanel.htm?" + "loveis" + panelyear + panelmonth + padout(i) + ".jpg" + "&" + panelyear + "&" + panelmonth + "&" + padout(i) + "&" + paneldir + "/" + "'>");
    document.write('<font color="#b70600" size="3" face="comic sans MS"><b><i>');
    document.write(padout(i));
    document.write('</a></font></b></i>');
    document.write('</td>');
    }
    }

    if i change the following:

    var nopanelStr = "." + nopanels.join(".") + ".";
    if (nopanelStr.indexOf("." + i + ".") != -1)

    to:
    if (i == 2) // or some other value it works fine

    and if i change this:
    case 1 : var nopanels = new array(1, 2, 3, 4, 11, 17, 18, 19, 25, 29, 31); break;

    to this:
    nopanels = [];
    nopanels[1970][1] = [1, 2, 3, 4, 11, 17, 18, 19, 25, 29, 31];

    how would it be called using the .join and indexOf statement....

    thanks,
    john....

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i know im close and its frustrating to figure out why its not working properly.... its right there in front of me and im not seeing it....

    thanks,
    john....

  12. #12
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay,

    Code:
    var nopanelStr = "." + nopanels.join(i) + "."; // incorrect in your source
    
    //should be
    var nopanelStr = "." + nopanels.join(".") + ".";
    Instead of using all those switch cases to set nopanels, jut use
    Code:
    var nopanelStr = "." + (nopanels[Year][Month]).join(".") + ".";
    
    //e.g. if Year = 1970 and Month = 1 it would execute
    var nopanelStr = "." + (nopanels[1970][1]).join(".") + "."

  13. #13
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a distraction: If the possible years are after 1900 and before 2100 you can simplify your leap year code (because 2000 is a leap year)
    Code:
    days[1] =  (Year &#37; 4 == 0) ? 29 : 28;

  14. #14
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IT WORKS, IT WORKS!!!!!!!!!!!!!!!!!!!!!

    thank you for all your help, THANK YOU.... THANK YOU!!!!!!!!!!!!!

    sorry for being a pest (LOL)....

    thanks,
    john....

  15. #15
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's fantastic. Great news. Good luck with the site.

  16. #16
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you for all your help "r51".... now all that left is to do the decades of 1980-1989 and 1990-1999.... i have them all from january 2000 to present.... now i can get rid of nearly 120 .htm files and just have 2.... thanks again....

    john...


Bookmarks

Posting Permissions

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