Remove trailing whitespace.
Use document.URL for base if document.baseURI is unavailable.

Addresses #324.