Fix ReferenceError issue when document is not defined