I'm maintaining a really old Web Forms application that is glued together with IE6 compatible javascript. This means that elements can be accessed in JS without needing to be initialised first.
What I mean is that using this is 'valid':
txtIngredient.value = 'potato';
In order to get this to work in newer browsers, like Chrome, Firefox, etc, this code becomes:
document.getElementById('txtIngredient').value = 'potato';
Obviously, Chrome, Firefox, etc, throw out an undefined error when encountering the former statement, and the JS stops executing due to the exception raised.
At the moment I'm running through the code and porting it over to make it look more like the latter, but I was wondering, could I write an error handler to handle undefined errors, which would catch when these situations occur, and then somehow return that element if it found it in the DOM and re-attempt the statement? If so this would save a lot of work.
EDIT: For example:
This code won't work:
function ShrinkDetails() {
document.Form1.btnExpand.value = "+ Dispense Details";
trDateDispensed.style.display = "none";
trDuration.style.display = "none";
trStartDate.style.display = "none";
trRepeats.style.display = "none";
trQuantity.style.display = "none";
}
Chrome etc throws an undefined error on document.Form1.
This code would work though:
function ShrinkDetails() {
document.getElementById('btnExpand').value = "+ Dispense Details";
document.getElementById('trDateDispensed').style.display = "none";
document.getElementById('trDuration').style.display = "none";
document.getElementById('trStartDate').style.display = "none";
document.getElementById('trRepeats').style.display = "none";
document.getElementById('trQuantity').style.display = "none";
}
Sounds like you need to do a try/catch for the reference error that you would get?