简体   繁体   English

禁用Wordpress上的javascript文件的最佳方法是什么?

[英]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. 我正在处理wordpress上的儿童主题,我想在移动设备和平板电脑视口上禁用Js。 Which method is the best : 哪种方法最好:

  • to load my javascripts on functions.php and use wp_is_mobile of wordpress to enqueue or dequeue ? 加载我的javascripts在functions.php上,并使用wordpress的wp_is_mobile入队或出队?

  • Or load my javascripts in the header.php and use 或将我的JavaScript加载到header.php中并使用

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. 我同意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: 如果脚本是由父主题使用wp_enqueue_script函数加载的,则可以将其添加到子主题函数文件中:

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. 我会在使文件入队之前添加检查,而不仅仅是在之后使它们出队。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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