简体   繁体   中英

What is the best way to disable a javascript files on Wordpress?

I'm working on a child theme on wordpress and i would like to disable a Js on mobile and tablets viewports. Which method is the best :

  • to load my javascripts on functions.php and use wp_is_mobile of wordpress to enqueue or dequeue ?

  • Or load my javascripts in the header.php and use

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 // some code..
}

?

I'm a newbie, and all i'm testing doesn't work.

I agree with the comment from adeneo. If the scripts are being loaded by the parent theme using the wp_enqueue_script function you could add this to your child themes functions file:

function scrjsenqeue() { 
  if( !wp_is_mobile() ) {
      wp_enqueue_script( 'scroll_magic', get_stylesheet_directory_uri() . '/js/scrollmagic.js', array( 'jquery' ),'1.0.0',true );
  } 
} 
add_action( 'wp_enqueue_scripts', 'scrjsenqeue' );

I would add the check before enqueueing the files and not just dequeue them after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM