How to Remove Unused JavaScript in WordPress to Improve Website Performance and User Experience

Before removing unused JavaScript, we need to first identify the specific source of these scripts. Tools such as PageSpeed Insights and GTmetrix can be used can help identify JavaScript that is unnecessary to the loading of the page.

图片[1]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度

1. Use PageSpeed Insights to Identify Unused JavaScript

PageSpeed Insightsis a free tool provided by Google to help analyze website performance and provide optimization recommendations. Here are the steps to find out unused JavaScript using PageSpeed Insights:

  • Step 1: Access PageSpeed InsightsThe
  • Step 2: Enter the URL of the WordPress page you want to analyze.
图片[2]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度
  • Step 3: click "analyze" button and wait for the analysis to complete.
  • Step 4: in "diagnostic" section, look for the entry "Reduce unused JavaScript".
图片[3]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度
  • Step 5: Look at the listed JavaScript files to identify which scripts are not important to the functionality of the page.

PageSpeed Insights flags the pages that are considered to beUnused specific JavaScript files.. This can be analyzed on a case-by-case basis to see what the files are used for and to see if these scripts can be removed without affecting core functionality.

For example."analytics.js","tracking.js" may be used for statistics and tracking, and if the page doesn't need these features, consider removing them.

2. Using GTmetrix to find unused JavaScript

GTmetrix is also a powerful website performance diagnostic tool. Below are the specific steps:

  • Step 1: Access GTmetrixEnter your WordPress website URL.
  • Step 2: Click on "Test your site" to run the analysis.
图片[4]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度
  • Step 3: in "Main issues"Partially, look for "Reduce JavaScript execution time".
  • Step 4: click "waterfalls" tag to see a list of JavaScript files loaded by the page.
图片[5]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度
  • Step 5: Flag JS files that take a long time to load and are not core functionality related and consider removing these scripts.

It is possible to useDeveloper Tools for Browsers, such as Chrome's "probe"The "Network" tab allows you to view the JavaScript scripts loaded by a website to further identify the source of unused scripts.

图片[6]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度

How to remove unused JavaScript from WordPress?

Once you have identified the JavaScript that needs to be optimized, you can remove these unnecessary scripts through plugins or manual methods.

Method 1: Use plug-ins to reduce or delay JavaScript execution

WordPress plugins can easily help you handle various functions of your website, also including reducing or delaying the execution of JavaScript scripts. Recommended plugins include:

  • Asset CleanUp: Automatically scan and optimize unused JS, CSS resources.
  • WP Rocket: Provides performance optimizations such as asynchronous loading and delayed JavaScript.

How to use these plugins:

  1. Installation and activation: Install and activate from the WordPress plugin repository, e.g. Asset CleanUpThe
图片[7]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度
  1. JS Management: The plugin automatically scans and lists unused resources.
图片[8]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度
  1. Inspection and optimization: Disable or remove unnecessary scripts and style sheets.
  2. Configuring Plug-ins: Adjust asynchronous loading and delayed execution settings as needed for best optimization.

The plugin automatically handles unused scripts and provides performance optimizations without the need for manual user intervention, which is convenient.

Method 2: Delete unused JavaScript manually

If you wishWithout plug-insto optimize performance, which can be done using manual methods. The following are the specific steps:

  1. Creating subtopics: Avoid modifying code directly in the parent theme to avoid losing changes when updating.
  2. Open functions.php: insubthemefolder, locate the functions.php file.
图片[9]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度
  1. Add code to remove scripts::
function my_remove_scripts() { wp_dequeue_script('script_handle_name'); } add_action('wp_enqueue_scripts', 'my_remove_scripts', 20); 
  1. commander-in-chief (military) script_handle_name Replace it with the name of the actual script handle to be deleted. The handle can be found in the HTML source code.
  2. Test site: After modifications, check the site functionality and layout to make sure there are no problems.

The manual method is more flexible, but may involve some technical maneuvering. If you are not familiar with editing WordPress core files, it is recommended to consult a developer.

Benefits of Removing Unused JavaScript

图片[10]-如何删除未使用的JavaScript来优化WordPress性能,提高页面加载速度

By removing unnecessary JavaScript files, your WordPress site will get a significant performance boost:

  1. Speed up page loading: Removing unnecessary scripts can significantly reduce load times.
  2. Enhancing the user experience: Increased loading speed leads to a better user experience and lower bounce rate.
  3. Improve SEO performance: Search engines prioritize sites that load quickly, so site rankings may improve.
  4. Reduced server costs: Reduce unnecessary requests and bandwidth consumption, and reduce server stress.
  5. Reducing security vulnerabilities: Fewer scripts means fewer potential points of attack, enhancing site security.
  6. Optimize the mobile experience: Unused JavaScript has a large impact on mobile, and removing them improves the browsing experience for mobile users.

Frequently Asked Questions

Is it safe to delete unused JavaScript?

Removing unused JavaScript is safe as long as care is taken. After modifying the code, be sure to test the site to make sure the functionality is working properly.

How often should I check for unused JavaScript?

Regular checking is recommended, especially after installing or updating plugins, themes, as new features may add unused JavaScript.

Is it risky to remove JavaScript manually?

Removing scripts manually can pose a risk, so if you're not sure, use a plugin or consult a professional developer for help.


Contact Us
Can't read the article? Contact us for free answers! Free help for personal, small business sites!
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
Author: xiesong
THE END
If you like it, support it.
kudos11 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments