Added a check for the existence of DOM object.