Fix error when document is undefined