...

View Full Version : IE7 error: "Object does not support this property or method"



name _F1
08-25-2007, 12:11 PM
I've been working on a Javascript time converter for use on my website. It's working perfectly on Firefox and Opera, but throws errors in Internet Explorer 7 (haven't tested on IE6). I can't see any problems with the code. Is there some issue with it that I haven't noticed?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function convert() {
toTime = document.getElementById("timezoneTo");
fromTime = document.getElementById("timezoneFrom");
hours = Number(document.getElementById("hours").value);
minutes = Number(document.getElementById("minutes").value);
pm = document.getElementById("ampm").selectedIndex == 1;

if (pm)
hours = (hours == 12) ? 12 : hours+12;
else
hours = (hours == 12) ? 0 : hours;

num = toTime.options[toTime.selectedIndex].value * 60 - fromTime.options[fromTime.selectedIndex].value * 60;
num += hours * 60;
num += minutes;

if (isNaN(num)) { num = 0; }
minutes = num % 60;
num -= minutes;
hours = num / 60;

if (hours < 0) { hours+=24; }
while (minutes < 0) { minutes += 30; }
while (hours >= 24) { hours-=24; }
ampm = hours >= 12 ? "PM" : "AM";
if (hours > 12)
hours -= 12;
if (hours == 0) { hours = 12; }
minutes = formatTime(minutes);
document.getElementById("converted").innerHTML = hours+":"+minutes+" "+ampm;
}

function formatTime(i) {
if (i<10)
{i = "0" + i}
return i
}
</script>
</head>
<body onload="convert();">
<div class="box" id="convert">
<span class="header">Time Converter</span>
<div style="margin: 0pt auto;">
<table>
<tr>
<td>Time to convert: </td>
<td>
<input size="2" maxlength="2" title="Time (hours)" name="hours" id="hours" value="" onkeyup="convert()" type="text">:<input size="2" maxlength="2" name="minutes" id="minutes" title="Time (minutes)" value="" onkeyup="convert()" type="text">
<select name="ampm" id="ampm" onchange="convert()">
<option value="0" selected="selected">AM</option>
<option value="1">PM</option>
</select>
</td>
</tr>
<tr>
<td>Converted time:</td>
<td><span id="converted">12:00 PM</span></td>
</tr>
</table>
<table>
<tr>
<td>From time zone:</td>
<td>
<select name="timezoneFrom" id="timezoneFrom" onchange="convert();">
<option value="-12">(GMT - 12:00 hours) Enitwetok, Kwajalien</option>
<option value="-11">(GMT - 11:00 hours) Midway Island, Samoa</option>
<option value="-10">(GMT - 10:00 hours) Hawaii</option>
<option value="-9">(GMT - 9:00 hours) Alaska</option>
<option value="-8">(GMT - 8:00 hours) Pacific Time (US &amp; Canada)</option>
<option value="-7">(GMT - 7:00 hours) Mountain Time (US &amp; Canada)</option>
<option value="-6">(GMT - 6:00 hours) Central Time (US &amp; Canada), Mexico City</option>
<option value="-5">(GMT - 5:00 hours) Eastern Time (US &amp; Canada), Bogota, Lima</option>
<option value="-4">(GMT - 4:00 hours) Atlantic Time (Canada), Caracas, La Paz</option>
<option value="-3.5">(GMT - 3:30 hours) Newfoundland</option>
<option value="-3">(GMT - 3:00 hours) Brazil, Buenos Aires, Falkland Is.</option>
<option value="-2">(GMT - 2:00 hours) Mid-Atlantic, Ascention Is., St Helena</option>
<option value="-1">(GMT - 1:00 hours) Azores, Cape Verde Islands</option>
<option value="0">(GMT) Casablanca, Dublin, London, Lisbon, Monrovia</option>
<option value="1">(GMT + 1:00 hours) Brussels, Copenhagen, Madrid, Paris</option>
<option value="2">(GMT + 2:00 hours) Kaliningrad, South Africa</option>
<option value="3">(GMT + 3:00 hours) Baghdad, Riyadh, Moscow, Nairobi</option>
<option value="3.5">(GMT + 3:30 hours) Tehran</option>
<option value="4">(GMT + 4:00 hours) Abu Dhabi, Baku, Muscat, Tbilisi</option>
<option value="4.5">(GMT + 4:30 hours) Kabul</option>
<option value="5">(GMT + 5:00 hours) Ekaterinburg, Karachi, Tashkent</option>
<option value="5.5">(GMT + 5:30 hours) Bombay, Calcutta, Madras, New Delhi</option>
<option value="6">(GMT + 6:00 hours) Almaty, Colomba, Dhakra</option>
<option value="7">(GMT + 7:00 hours) Bangkok, Hanoi, Jakarta</option>
<option value="8">(GMT + 8:00 hours) Hong Kong, Perth, Singapore, Taipei</option>
<option value="9">(GMT + 9:00 hours) Osaka, Sapporo, Seoul, Tokyo, Yakutsk</option>
<option value="9.5">(GMT + 9:30 hours) Adelaide, Darwin</option>
<option value="10">(GMT + 10:00 hours) Melbourne, Papua New Guinea, Sydney</option>
<option value="11">(GMT + 11:00 hours) Magadan, New Caledonia, Solomon Is.</option>
<option value="12">(GMT + 12:00 hours) Auckland, Fiji, Marshall Island</option>
</select>
</td>
</tr>
<tr>
<td>To time zone:</td>
<td>
<select name="timezoneTo" id="timezoneTo" onchange="convert();">
<option value="-12">(GMT - 12:00 hours) Enitwetok, Kwajalien</option>
<option value="-11">(GMT - 11:00 hours) Midway Island, Samoa</option>
<option value="-10">(GMT - 10:00 hours) Hawaii</option>
<option value="-9">(GMT - 9:00 hours) Alaska</option>
<option value="-8">(GMT - 8:00 hours) Pacific Time (US &amp; Canada)</option>
<option value="-7">(GMT - 7:00 hours) Mountain Time (US &amp; Canada)</option>
<option value="-6">(GMT - 6:00 hours) Central Time (US &amp; Canada), Mexico City</option>
<option value="-5">(GMT - 5:00 hours) Eastern Time (US &amp; Canada), Bogota, Lima</option>
<option value="-4">(GMT - 4:00 hours) Atlantic Time (Canada), Caracas, La Paz</option>
<option value="-3.5">(GMT - 3:30 hours) Newfoundland</option>
<option value="-3">(GMT - 3:00 hours) Brazil, Buenos Aires, Falkland Is.</option>
<option value="-2">(GMT - 2:00 hours) Mid-Atlantic, Ascention Is., St Helena</option>
<option value="-1">(GMT - 1:00 hours) Azores, Cape Verde Islands</option>
<option value="0" selected="selected">(GMT) Casablanca, Dublin, London, Lisbon, Monrovia</option>
<option value="1">(GMT + 1:00 hours) Brussels, Copenhagen, Madrid, Paris</option>
<option value="2">(GMT + 2:00 hours) Kaliningrad, South Africa</option>
<option value="3">(GMT + 3:00 hours) Baghdad, Riyadh, Moscow, Nairobi</option>
<option value="3.5">(GMT + 3:30 hours) Tehran</option>
<option value="4">(GMT + 4:00 hours) Abu Dhabi, Baku, Muscat, Tbilisi</option>
<option value="4.5">(GMT + 4:30 hours) Kabul</option>
<option value="5">(GMT + 5:00 hours) Ekaterinburg, Karachi, Tashkent</option>
<option value="5.5">(GMT + 5:30 hours) Bombay, Calcutta, Madras, New Delhi</option>
<option value="6">(GMT + 6:00 hours) Almaty, Colomba, Dhakra</option>
<option value="7">(GMT + 7:00 hours) Bangkok, Hanoi, Jakarta</option>
<option value="8">(GMT + 8:00 hours) Hong Kong, Perth, Singapore, Taipei</option>
<option value="9">(GMT + 9:00 hours) Osaka, Sapporo, Seoul, Tokyo, Yakutsk</option>
<option value="9.5">(GMT + 9:30 hours) Adelaide, Darwin</option>
<option value="10">(GMT + 10:00 hours) Melbourne, Papua New Guinea, Sydney</option>
<option value="11">(GMT + 11:00 hours) Magadan, New Caledonia, Solomon Is.</option>
<option value="12">(GMT + 12:00 hours) Auckland, Fiji, Marshall Island</option>
</select>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>

I'd also appreciate any comments if I am converting the time inefficiently, or there is a better way to do something.

Mr J
08-25-2007, 01:18 PM
You have ID names the same as variables I do not think IE allows this

Here's few snippets from your code

hours = Number(document.getElementById("hours").value);

minutes = Number(document.getElementById("minutes").value);

pm = document.getElementById("ampm").selectedIndex == 1;


ampm = hours >= 12 ? "PM" : "AM";

name _F1
08-25-2007, 01:28 PM
Thank you! I figured it must be a problem with the variable names but I couldn't figure out exactly what the problem was. I'll know to avoid this in the future.

glenngv
08-26-2007, 01:18 AM
I think it will make IE happy if you make the hours variable local by using the var keyword. Remember that if you don't declare a variable with a var keyword, it is considered global and all id'ed and named tags in IE can be accessed directly because IE also treats them as global variables. I think what's happening there is IE gets confused because IE sees both your variable hours and the id hours in global scope. It doesn't know which variable you are referring to.


function convert() {
toTime = document.getElementById("timezoneTo");
fromTime = document.getElementById("timezoneFrom");
var hours = Number(document.getElementById("hours").value);
...
}
But it is indeed a good programming practice to avoid identical names/ids and variables.

Danaconda36
09-01-2007, 10:49 AM
Avex DVD to Mobile Converter converts DVD movies to Mobile Phone 3GP format and let you watch mobile movies on the road.

http://www.mobile-video-converter.com/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum