Alexander Solonik
Alexander Solonik

Reputation: 10240

Understanding check for 'undefined' document

I was just going through the code of timesheet.js and came across the following lines of code:

if (typeof document !== 'undefined') {
      this.container = (typeof container === 'string') ? document.querySelector('#'+container) : container;
      this.drawSections();
      this.insertData();
    }

I totally understand what's going on , except the below line:

if (typeof document !== 'undefined') {

When and Why would the document ever be undefined ? What security does this condition add to the plugin ?

I don't understand When and Why would the document ever be undefined ? can anybody explain ?

Thank you.

Alex-z.

Upvotes: 0

Views: 48

Answers (2)

Harshal Carpenter
Harshal Carpenter

Reputation: 506

It is related to window.document. timesheet.js must using window.document for its purposes. Open your JS console and type document or window.document. If that component is undefined, timesheet will not work.

Upvotes: 0

Mike Cluck
Mike Cluck

Reputation: 32511

document would be undefined if you were running it in a non-browser environment such as Node.js.

Upvotes: 3

Related Questions