Skip to content

Magento 2.4 – Prevent widget from loading on mobile

We have an instagram widget on our site.
Perfect for desktops – but very negative for the pagespeed score on mobile.

So I would like to prevent it from loading on mobile.
Not hide it with {display:none;} (than it’s still being loaded in the background)

A friend wrote this script for me, but that does not work.

 <script>
    let details = navigator.userAgent;

    let regexp = /android|iphone|kindle|ipad/i;

    let isMobileDevice = regexp.test(details);

    if (!isMobileDevice) {
        document.write("{{widget type="WeltPixelInstagramWidgetBlockWidgetInstagram" instagram_api_type="basic_api" images_in_new_tab="1" images_per_row="col-5" images_have_padding="0" images_alt_text="1" images_have_caption="0" optimize_image_layout="1" enable_lazyload="1" limit="5" }}");
    } 
</script>

I added he widget code into this between the ” “, but that does not work and gives "); } as output on all devices.

My friend is currently unavailable. Can someone help me solve this?