Problems with the (Real) Live Textile Formatting
As I mentioned earlier I implemented the (Real) Live Textile Formatting (RLTF) to enable comment previews. I thought all was honky-dory until I tried working with some hefty javascript which was also initialized by the onload function of the body. My javascript initialize function was set to be called after the RLTF initialization, and it wouldn’t get called. A day and a half of debugging and I found the error which was in the javascript for the RLTF.
The RLTF is initialized by the liveReqInit function. In this function the “key down” (or whatever browser dependent) event is assigned to the “search field” using the addEventListener function. The problem occurs on a page where the search field doesn’t exist. The element is found using getElementById which returns null when the element doesn’t exist on the page, and the addEventListener is therefore called on a null object causing the javascript to crash. The crash means the chain of functions for the body onload will abort.
One solution is to check that the search field exists on the page. I do this by inserting the following code at the beginning of the liveReqInit:
var searchElement = document.getElementById(searchFieldId);
if( searchElement == null ) return;
Similarly, we should check that the result field exist by inserting
var resultElement = document.getElementById(resultFieldId);
if( resultElement == null ) return;
before if(emptyString == '') { near the end of the function.
I hope my headaches will prevent some for others.
May 19th, 2005 at 12:03 pm
делаем проверку на русском