I am showing/hiding div element with this code. It works fine, but once the app goes full screen, the div element stops updating. Ie it seems that when full screen, the changes in css are not working/updating anymore
document.addEventListener("keypress", function (event) {
if (event.keyCode == 122) {
disableCartModal();
}
if (event.keyCode == 120) {
enableCartModal();
}
});
function disableCartModal() {
document.getElementById("cart-modal").style.display = "none";
}
function enableCartModal() {
document.getElementById("cart-modal").style.display = "block";
}
Fullscreen code
document.addEventListener('fullscreenchange', exitHandler); document.addEventListener('webkitfullscreenchange', exitHandler); document.addEventListener('mozfullscreenchange', exitHandler); document.addEventListener('MSFullscreenChange', exitHandler);
function exitHandler() {
var element = document.documentElement;
if (element.requestFullScreen) {
element.requestFullScreen();
} else if (element.mozRequestFullScreen) {
element.mozRequestFullScreen();
} else if (element.webkitRequestFullScreen) {
element.webkitRequestFullScreen();
}
}
Edit: I am running javascript from Unity WebGL
4
Answers
Since this is a Unity WebGL project, I was assuming that all the html/javascript code is a separate thing. But I was switching fullscreen with Unity Screen.fullscreen, which essentially completely blocked any updates to css. So if by some chance anyone else comes to a situation, where they have Unity WebGL, that has to be full screen and they want to edit css through javascript called from Unity, make sure to trigger full screen from javascript itself, not from Unity.
As
event.keyCode
is deprecated (see here) you could try to check forevent.key
(see values here).Maybe your browser has dropped support or has the keys in question reserved for special actions.
I tried out your code and found it to work in both Firefox and Chrome… I added some debugging in the console to ensure things were firing as expected. I would suggest adding some debug statements to your code to make sure that you are hitting what you expect.
Also ensure that the part of the document you are testing has focus – testing, I found that if there are frames/ developer tools selected, the event would not be processed.
Also note, as Bastian points out, this is NOT the recommended way to do keyboard events in modern browsers. Support may disappear.
Modern method:
My experiment with your code (debug statements added):
I don’t think you quite understand the meaning of a minimal reproducible example. It doesn’t help to just add a tiny bit of code to your question, you should try to create a complete example, and test it yourself, so we can work with it to try to reproduce your problem.
You can run my attempt below, even full screen, and you’ll see it works just fine. At least, it does for me.