Preventing undefined exception on document click