... var ObjDiv = document.getElementById(Layer); if(!ObjDiv)return; // salida prematura si el elemento no existe; ...