Javascript OpenFullscreen during startup

Hello,

in Blazor Server I would like to switch the browser window to fullscreen mode when the user of the application changes. To be precise the user can choose in the options of the application if the window should be shown fullscreen.

I use the javascript code from https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_element_requestfullscreen_exit which works.

I inject IJSRuntime and call the code which works also.
jsRuntime.InvokeVoidAsync("OpenFullscreen");

But I do not know in which method to call the code when the user changes. I am able to check if the user changed in OnInitializedAsync in MainLayout.razor.cs. The check works well but the OpenFullscreen does not show any effect.

Hopefully someone can help.
THANX
Tom

You could try the OnAfterRenderAsync lifecycle method instead of OnInitializedAsync. JSRuntime may not be active during OnInitializedAsync.

Thanks for your answer. I tried without success. For your information:

In Firefox the method seems to take too long:

Anfrage für Vollbildmodus wurde abgelehnt, weil Element.mozRequestFullScreen() nicht aus einem kurz laufenden Benutzer-generierten Ereignis-Handler aufgerufen wurde. MBDS.Functions.js:6:9
Uncaught (in promise) TypeError: Fullscreen request denied

In Chrome a user action seems to be necessary:

MBDS.Functions.js:6 Failed to execute 'requestFullscreen' on 'Element': API can only be initiated by a user gesture.
OpenFullscreen @ MBDS.Functions.js:6

MBDS.Functions.js:6 Uncaught (in promise) TypeError: fullscreen error

I will not continue to implement the automatic fullscreen mode change.

It seems from the Chrome error message that this API can't be invoked without an user action first - e.g. a button click. It is probably considered a security risk.