Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Yonibrese's avatar

Help with mobile screen orientation actions

Hello, I have made a mobile view for a simple webpage, I have one issue though I have an image where what I want to happen is when a user touches the image it goes full screen and the orientation of the screen changes from "portrait" to "landscape" and only while on a mobile device.

I have added the following code .

HTML/Blade:

<div class="Image_block" id="Image4" ontouchstart=changeOriantaion(this.id)>
                                <img src="{{URL::asset('/assets/images/slider_images/imageSize920x920.png')}}"
                                    class="d-block w-100">
</div>

JS:

function changeOriantaion(elementID){
    if(navigator.userAgentData.mobile){
        document.querySelector(`#${elementID}`).requestFullscreen()
        screen.orientation.lock("landscape")
    }
}

where the elementID is the ID of the image being touched.

While testing on chrome dev tools mobile view the image becomes full screen and I get the following error :

Uncaught (in promise) DOMException: screen.orientation.lock() is not available on this device."

This is an expected error because a desktop browser cannot change its orientation.

The following is a link to the codepen I made of this issue: Link here

When I test this on my a mobile device there is not change at all. The image does not go full screen and there is no orientation change.

can anyone help with this issue? what is missing in my code?

Thanks in advance

0 likes
1 reply
Yonibrese's avatar
Yonibrese
OP
Best Answer
Level 1

Hey I found the solution

The if statement

 if(navigator.userAgentData.mobile){
// do something
}

was incorrect.

I originally added that code in there as a check that we are only on a mobile device, but the touch event does most of the same thing, a touch event will only fire on a touchscreen device (most of those are mobile).

Please or to participate in this conversation.