user2958542
user2958542

Reputation: 341

Immediately add eventListener to page TypeScript

I'm working on a project where I want an event listener to be on the page when the page loads to wait for a message (a message from an external source will be sent instantly to this page when this page loads, so it has to be able to listen for it). Is there a way to do this in a TypeScript module? or does it have to be external of the module?

Possibly something like:

module MyModule {
    export class MyClass{
        constructor { ... }
        public addEventListeners() : void { ... }
        ...
    }
}

window.onload = () => {
    var class: MyClass = new MyClass();
    class.addEventListeners();
};

Upvotes: 1

Views: 117

Answers (2)

Artem
Artem

Reputation: 1880

You should specify module name and use something else from class which is reserved word:

window.onload = () => {
    var classInstance = new MyModule.MyClass();
    classInstance.addEventListeners();
};

Upvotes: 1

basarat
basarat

Reputation: 276363

window.onload will work fine. Recommend checking document.readyState to make sure that it is not already completed (needed for lazy loading a page).

Take a look at Jquery Ready source

Upvotes: 0

Related Questions