...

View Full Version : works in IE only



Crash1hd
06-06-2003, 09:45 AM
The following script works in IE but for some reason it doesnt work in anyother browser any thoughts??


<html><head><title>New Page 1</title>
<SCRIPT language="JavaScript">
//Current date in form script
//By Lee Hinder (lee.hinder@ntlworld.com)
//Visit http://javascriptkit.com for this script and more

//set todays date

Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();
if (NowYear < 2000) NowYear += 1900; //for Netscape

//function for returning how many days there are in a month including leap years

function DaysInMonth(WhichMonth, WhichYear)
{
var DaysInMonth = 31;

if (WhichMonth == "April" || WhichMonth == "June" || WhichMonth == "September" || WhichMonth == "November") DaysInMonth = 30;
if (WhichMonth == "February" && (WhichYear/4) != Math.floor(WhichYear/4)) DaysInMonth = 28;
if (WhichMonth == "February" && (WhichYear/4) == Math.floor(WhichYear/4)) DaysInMonth = 29;
return DaysInMonth;
}

//function to change the available days in a months

function ChangeOptionDays(Which)
{
DaysObject = eval("document.Form1." + Which + "Day");
MonthObject = eval("document.Form1." + Which + "Month");
YearObject = eval("document.Form1." + Which + "Year");

Month = MonthObject[MonthObject.selectedIndex].text;
Year = YearObject[YearObject.selectedIndex].text;

DaysForThisSelection = DaysInMonth(Month, Year);

CurrentDaysInSelection = DaysObject.length;

if (CurrentDaysInSelection > DaysForThisSelection)
{
for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
{
DaysObject.options[DaysObject.options.length - 1] = null
}
}

if (DaysForThisSelection > CurrentDaysInSelection)
{
for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
{
//DaysObject.options[DaysObject.options.length + 1] = null
NewOption = new Option(DaysObject.options.length + 1);
DaysObject.add(NewOption);
}
}

if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}


//function to set options to today

function SetToToday(Which)
{

DaysObject = eval("document.Form1." + Which + "Day");
MonthObject = eval("document.Form1." + Which + "Month");
YearObject = eval("document.Form1." + Which + "Year");

YearObject[1].selected = true;
MonthObject[NowMonth].selected = true;

ChangeOptionDays(Which);

DaysObject[NowDay-1].selected = true;
}


//function to write option years plus x

function WriteYearOptions(YearsAhead)
{
line = "";

for (i=0; i<YearsAhead; i++)
{
line += "<OPTION>";
line += (NowYear + 1) - i;
}

return line;
}

</SCRIPT>
</head>

<body><%'onload="SetToToday('FirstSelect');"'this doesnt work in netscape%>
<FORM name="Form1">

<SELECT name="FirstSelectMonth" onchange="ChangeOptionDays('FirstSelect')">
<OPTION>January</option>
<OPTION>February</option>
<OPTION>March</option>
<OPTION>April</option>
<OPTION>May</option>
<OPTION>June</option>
<OPTION>July</option>
<OPTION>August</option>
<OPTION>September</option>
<OPTION>October</option>
<OPTION>November</option>
<OPTION>December</option>
</SELECT>
<SELECT name="FirstSelectDay">
<OPTION>01</option>
<OPTION>02</option>
<OPTION>03</option>
<OPTION>04</option>
<OPTION>05</option>
<OPTION>06</option>
<OPTION>07</option>
<OPTION>08</option>
<OPTION>09</option>
<OPTION>10</option>
<OPTION>11</option>
<OPTION>12</option>
<OPTION>13</option>
<OPTION>14</option>
<OPTION>15</option>
<OPTION>16</option>
<OPTION>17</option>
<OPTION>18</option>
<OPTION>19</option>
<OPTION>20</option>
<OPTION>21</option>
<OPTION>22</option>
<OPTION>23</option>
<OPTION>24</option>
<OPTION>25</option>
<OPTION>26</option>
<OPTION>27</option>
<OPTION>28</option>
<OPTION>29</option>
<OPTION>30</option>
<OPTION>31</option>
</SELECT>
<SELECT name="FirstSelectYear" onchange="ChangeOptionDays('FirstSelect')">
<SCRIPT language="JavaScript">
document.write(WriteYearOptions(200));
</SCRIPT>
</SELECT>

</FORM>

</body>

</html>


The Error I get in Netscape its
JavaScript Error: test.asp,
line 60:

DaysObject.add is not a function.

and In Netscape 7 its

Error: uncaught exception: [Exception... "Not enough arguments [nsIDOMHTMLSelectElement.add]" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: http://www.alwaysremember.ca/test.asp :: ChangeOptionDays :: line 60" data: no]

in Opera its

Error:
name: TypeError
message: Attempted to call non-function: has undefined value: 'add'

Line 60 is in blue

Vladdy
06-06-2003, 12:42 PM
Sure, it won't work in any other browser since it uses IE proprietoty methods to access document objects.
Solution: use DOM

BTW, what does script do?

ca_redwards
06-06-2003, 04:08 PM
I created a fully-navigable cross-browser popup calendar (http://www.angelfire.com/ca/redwards/html__.calendar.html#examples) for selecting a date (which automatically populates a readonly textfield in MM/DD/YYYY format).

This bit of JavaScript is just under 4K. :D

If you find it potentially useful, let me know and I may even help you tailor it for use in your application.

:ca_redwards.

Crash1hd
06-06-2003, 08:19 PM
THanks for the replys the above creates 3 drop down fields one for month day and year and on check of the month and year fields the day field changes acordingly so if its january the day field has 31 and if its june its 30 if its feb of 2003 then its 28 if its feb 2004 its 29 needs to be dynamic though like the above one does I have seen others where you put in the years manually but the above one checks the current year and goes down by 100 and it checks the day and goes up by 30 :cool:

I changed the day one to use this

function WriteDayOptions(DaysAhead)
{
line = "";

for (i=0; i<DaysAhead; i++)
{
line += "<OPTION>";
line += (NowDay + 1) - i;
}

return line;
}

and then in the body
<SELECT name="FirstSelectDay">
<SCRIPT language="JavaScript">
document.write(WriteDayOptions(31));
</SCRIPT>
</SELECT>



What doesnt make sense is that the following part of the code works in netscape

if (CurrentDaysInSelection > DaysForThisSelection)
{
for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
{
DaysObject.options[DaysObject.options.length - 1] = null
}
}

but to go the opposite way it doesnt work in netscape or ie there must be something I can put in here

if (CurrentDaysInSelection < DaysForThisSelection)
{
for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
{
In Here
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum