Firefox generally chokes with hoisting function declarations in blocks... You can try a simple example yourself:
if (true) {
execute();
function execute() { alert("worked"); }
}
The above works in Chrome and IE but doesn't in FF (although there is an error). Try putting the declaration of EMBEDformatAMPM
outside of your if:
function EMBEDformatAMPM() {
var d = new Date(),
minutes = d.getMinutes().toString().length == 1 ? '0' + d.getMinutes() : d.getMinutes(),
hours = d.getHours().toString().length == 1 ? '0' + d.getHours() : d.getHours(),
ampm = d.getHours() >= 12 ? 'pm' : 'am',
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
return days[d.getDay()] + ' ' + d.getDate() + ' ' + months[d.getMonth()] + ' ' + d.getFullYear();
}
var el = document.getElementById('currentdate');
if (el != null) {
el.innerHTML = EMBEDformatAMPM();
}
I may be a being slightly unfair to say that FF chokes - according to the spec the only thing allowed within a block are statements and a function declaration is not a statement.
You may also want to head over to code review as there are several things in your code that could be improved.
Edit (replying to comments)
It's not a bug with Firefox as what you are doing is not technically allowed according to the specification. Chrome and IE are being more permissive by making a good guess at what you want but there's no guarantee that it will work in future versions.
As to improvements to your code, check out this fiddle
// wrap in IIFE to avoid polluting global namespace.
(function () {
// declare all our variables at the top of the function
var currentDate = document.getElementById('currentdate'),
currentTime = document.getElementById('currenttime'),
updateTime,
getFormattedDate,
getFormattedTime;
// Give functions names that make their purpose clear
getFormattedDate = function() {
var d = new Date(),
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
return days[d.getDay()] + ' ' + d.getDate() + ' ' + months[d.getMonth()] + ' ' + d.getFullYear();
};
getFormattedTime = function () {
var d = new Date(),
minutes = d.getMinutes().toString().length == 1 ? '0' + d.getMinutes() : d.getMinutes(),
hours = d.getHours().toString().length == 1 ? '0' + d.getHours() : d.getHours();
return hours + ':' + minutes;
};
// This function updates the time and then
// sets a timeout to execute again after 3 seconds.
updateTime = function () {
if (currentTime !== null && currentDate !== null) {
currentTime.innerHTML = getFormattedTime();
currentDate.innerHTML = getFormattedDate();
}
window.setTimeout(updateTime, 3000);
};
// Start it off.
updateTime();
}());