Title: how to add javascript in WordPress?
Last modified: August 22, 2016

---

# how to add javascript in WordPress?

 *  [ron regev](https://wordpress.org/support/users/ron-regev/)
 * (@ron-regev)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/how-to-add-javascript-in-wordpress/)
 * hello,
 * im trying to add some script that change between menu classes.
    i am putting 
   the script in the header but its not working… i already read the java-script 
   usage but i didn’t understand :/ what i need to so?

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [BellaBerlin](https://wordpress.org/support/users/bellaberlin/)
 * (@bellaberlin)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/how-to-add-javascript-in-wordpress/#post-5740657)
 * You can put a snippet of inline code just anywhere in a page or post, and also
   in the header.php file. Try something like this to see if the script appears 
   in the page:
 *     ```
       <script>
       alert("Script is there!");
       </script>
       ```
   
 * Often it is better to put scripts in external .js files. WordPress has a mechanism
   to enqueue them, which also works nicely together with caching etc.
 *     ```
       function my_scripts() {
         wp_enqueue_script('my-script', get_template_directory_uri() . '/js/my-script.js', array(), '1.0.0', true );
       }
   
       add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
       ```
   
 *  Thread Starter [ron regev](https://wordpress.org/support/users/ron-regev/)
 * (@ron-regev)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/how-to-add-javascript-in-wordpress/#post-5740683)
 * hey bella, my script is:
 *     ```
       <script type='text/javascript'>
         var screenWidth = window.screen.width;
         var element = document.getElementById('site-navigation');
         if (screenWidth < 885) {
           element.className = main-navigaion;
         }
         else
         {
           element.className = main-navigaion2;
         }
       </script>
       ```
   
 * so you recommend to save it in external file and then **how **is the function
   my_scripts needs to look like?
    thank you.
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [11 years, 4 months ago](https://wordpress.org/support/topic/how-to-add-javascript-in-wordpress/#post-5740690)
 * So where exactly did you save your external file?
 *  Thread Starter [ron regev](https://wordpress.org/support/users/ron-regev/)
 * (@ron-regev)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/how-to-add-javascript-in-wordpress/#post-5740696)
 * public_html/wp/wp-content/themes/pictorico/js/menu2.js
 * thanks !
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [11 years, 4 months ago](https://wordpress.org/support/topic/how-to-add-javascript-in-wordpress/#post-5740701)
    1. Create a Child Theme: [http://codex.wordpress.org/Child_Themes#How_to_Create_a_Child_Theme](http://codex.wordpress.org/Child_Themes#How_to_Create_a_Child_Theme)
    2. In your Child Theme folder create a “js” folder
    3. Put your JS file inside that folder
    4. Create a functions.php file inside your Child Theme folder
    5. Inside your Child Theme functions.php file put this:
    6.     ```
           <?php
       
           function my_scripts() {
             wp_enqueue_script('my-script', get_stylesheet_directory_uri() . '/js/my-script.js', array(), '1.0.0', true );
           }
       
           add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
       
           ?>
           ```
       

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘how to add javascript in WordPress?’ is closed to new replies.

## Tags

 * [php wordpress java script html](https://wordpress.org/support/topic-tag/php-wordpress-java-script-html/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 3 participants
 * Last reply from: [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * Last activity: [11 years, 4 months ago](https://wordpress.org/support/topic/how-to-add-javascript-in-wordpress/#post-5740701)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
