Халид Хан Халид Хан - 4 months ago 37
Javascript Question

Uncaught TypeError: Illegal invocation javascript

how fix Illegal invocation when call function in this class

new FullScreen().request(); // Uncaught TypeError: Illegal invocation

new FullScreen().cancel(); //Uncaught TypeError: Illegal invocation

new FullScreen().toggle(); //Uncaught TypeError: Illegal invocation


import validator from "Validator";

export default class FullScreen {
constructor() {
if (validator.checkValueType(Modernizr.fullscreen, "BOOLEAN") && Modernizr.fullscreen) {
this.requestFullscreen = document.documentElement.requestFullscreen || document.documentElement.msRequestFullscreen ||
document.documentElement.mozRequestFullScreen || document.documentElement.webkitRequestFullscreen;
this.cancelFullScreen = document.cancelFullScreen || document.msCancelFullScreen || document.mozRequestFullScreen || document.webkitRequestFullscreen;
} else {
throw new UserException("Fullscreen not supported in this browser");
}

}

request() {
this.requestFullscreen();
}

cancel() {
this.cancelFullScreen();
}

isFullScreen() {
return document.isFullScreen || document.msIsFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen;
}

toggle() {
if (this.isFullScreen()) this.cancel();
else this.request();
}
}

Answer

Without testing it, my best guess is that you've lost the context of the document. I'd suggest shifting the document element into your methods.

import validator from "Validator";

export default class FullScreen {
    constructor() {
        if (!validator.checkValueType(Modernizr.fullscreen, "BOOLEAN") && Modernizr.fullscreen) {
            throw new UserException("Fullscreen not supported in this browser");
        }
    }

    request() {
        document.documentElement.requestFullscreen || document.documentElement.msRequestFullscreen ||
                document.documentElement.mozRequestFullScreen || document.documentElement.webkitRequestFullscreen;
    }

    cancel() {
        document.cancelFullScreen || document.msCancelFullScreen || document.mozRequestFullScreen || document.webkitRequestFullscreen;
    }

    isFullScreen() {
        return document.isFullScreen || document.msIsFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen;
    }

    toggle() {
        if (this.isFullScreen()) this.cancel();
        else this.request();
    }
}
Comments