'How to make google chrome go full screen in Angular 4 Application?
I am developing an application where I want to implement such a thing where if user leaves from one component & enters other component, then in other component's ngOnInit method chrome browser should go full screen same as when we press Fn + F11 Key.
Any help or references are appreciated.
Solution 1:[1]
You can try with requestFullscreen
I have create a demo on Stackblitz
fullScreen() {
let elem = document.documentElement;
let methodToBeInvoked = elem.requestFullscreen ||
elem.webkitRequestFullScreen || elem['mozRequestFullscreen']
||
elem['msRequestFullscreen'];
if (methodToBeInvoked) methodToBeInvoked.call(elem);
}
Solution 2:[2]
How To - Fullscreen - https://www.w3schools.com/howto/howto_js_fullscreen.asp
This is the current "angular way" to do it.
HTML
<h2 (click)="openFullscreen()">open</h2>
<h2 (click)="closeFullscreen()">close</h2>
Component
import { DOCUMENT } from '@angular/common';
import { Component, Inject, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(@Inject(DOCUMENT) private document: any) {}
elem;
ngOnInit() {
this.elem = document.documentElement;
}
openFullscreen() {
if (this.elem.requestFullscreen) {
this.elem.requestFullscreen();
} else if (this.elem.mozRequestFullScreen) {
/* Firefox */
this.elem.mozRequestFullScreen();
} else if (this.elem.webkitRequestFullscreen) {
/* Chrome, Safari and Opera */
this.elem.webkitRequestFullscreen();
} else if (this.elem.msRequestFullscreen) {
/* IE/Edge */
this.elem.msRequestFullscreen();
}
}
/* Close fullscreen */
closeFullscreen() {
if (this.document.exitFullscreen) {
this.document.exitFullscreen();
} else if (this.document.mozCancelFullScreen) {
/* Firefox */
this.document.mozCancelFullScreen();
} else if (this.document.webkitExitFullscreen) {
/* Chrome, Safari and Opera */
this.document.webkitExitFullscreen();
} else if (this.document.msExitFullscreen) {
/* IE/Edge */
this.document.msExitFullscreen();
}
}
}
Solution 3:[3]
put the following code on the top of the component (before @Component) you want to trigger:
interface FsDocument extends HTMLDocument {
mozFullScreenElement?: Element;
msFullscreenElement?: Element;
msExitFullscreen?: () => void;
mozCancelFullScreen?: () => void;
}
export function isFullScreen(): boolean {
const fsDoc = <FsDocument> document;
return !!(fsDoc.fullscreenElement || fsDoc.mozFullScreenElement || fsDoc.webkitFullscreenElement || fsDoc.msFullscreenElement);
}
interface FsDocumentElement extends HTMLElement {
msRequestFullscreen?: () => void;
mozRequestFullScreen?: () => void;
}
export function toggleFullScreen(): void {
const fsDoc = <FsDocument> document;
if (!isFullScreen()) {
const fsDocElem = <FsDocumentElement> document.documentElement;
if (fsDocElem.requestFullscreen)
fsDocElem.requestFullscreen();
else if (fsDocElem.msRequestFullscreen)
fsDocElem.msRequestFullscreen();
else if (fsDocElem.mozRequestFullScreen)
fsDocElem.mozRequestFullScreen();
else if (fsDocElem.webkitRequestFullscreen)
fsDocElem.webkitRequestFullscreen();
}
else if (fsDoc.exitFullscreen)
fsDoc.exitFullscreen();
else if (fsDoc.msExitFullscreen)
fsDoc.msExitFullscreen();
else if (fsDoc.mozCancelFullScreen)
fsDoc.mozCancelFullScreen();
else if (fsDoc.webkitExitFullscreen)
fsDoc.webkitExitFullscreen();
}
export function setFullScreen(full: boolean): void {
if (full !== isFullScreen())
toggleFullScreen();
}
and on the ngOnInit method make a call to the setFullScreen(full: boolean)
function:
ngOnInit(): void {
setFullScreen(true);
}
Solution 4:[4]
Most of the answer above are from 2018, nowdays to achieve compatibility in Chrome, Firefox, other browsers and mobile, I have determined that using requestFullScreen
is enough
https://developer.mozilla.org/en-US/docs/Web/API/Element/requestFullScreen
Here is an example on to be able to toggle between full screen and normal screen:
docElement: HTMLElement;
isFullScreen: boolean = false;
ngOnInit(): void {
this.docElement = document.documentElement;
}
toggleFullScreen() {
if (!this.isFullScreen) {
this.docElement.requestFullscreen();
}
else {
document.exitFullscreen();
}
this.isFullScreen = !this.isFullScreen;
}
The request to change to fullscreen can only be made when a user interacts with an element, for example a button
<button (click)="toggleFullScreen()">SWITCH SCREEN MODE</button>
Solution 5:[5]
This is the current "angular way" to do it.
HTML: Use This in Html:
(click)="openFullscreen()
NgOninit:
this.elem = document.documentElement;
TS: Put this Function it will work...
openFullscreen() {
if (this.elem.requestFullscreen) {
this.elem.requestFullscreen();
} else if (this.elem.mozRequestFullScreen) {
/* Firefox */
this.elem.mozRequestFullScreen();
} else if (this.elem.webkitRequestFullscreen) {
/* Chrome, Safari and Opera */
this.elem.webkitRequestFullscreen();
} else if (this.elem.msRequestFullscreen) {
/* IE/Edge */
this.elem.msRequestFullscreen();
}`]`
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Stavm |
Solution 3 | Ayoub k |
Solution 4 | |
Solution 5 | Abdull Rehman Memon |