-1

I want the following code to load only on desktop and tablet.

<script type="text/javascript" src="//wcs.naver.net/wcslog.js"></script>

Referring to this comment, I saved the code below as example.js and loaded it from the header.

$( document ). ready(function() {
     var isMobile = window.matchMedia("only screen and (min-width: 600px)");

     if (isMobile.matches) {
        **<script type="text/javascript" src="//wcs.naver.net/wcslog.js"></script>**
     }
  });

But the script does not loaded.

I'd like to hear some advice on how I should fix it.

Thank you

cheonmu
  • 61
  • 7

1 Answers1

0

Maybe the wp_is_mobile() function could help: https://developer.wordpress.org/reference/functions/wp_is_mobile/

I mean you can wrap the script tag within this condition and do it via PHP directly.

Abdelmonem
  • 51
  • 6
  • 1
    Hello. @Abelmonem Thanks for the reply. I know that funtion, but the post I linked to reads as follows. "Other answers have suggested using the WordPress function wp_is_mobile() when enqueuing a script. Be very careful doing this. If you use caching plugins or if your host caches PHP requests, this could make this method useless." So I'm looking for another way. – cheonmu Mar 26 '22 at 23:07