Title: Add Javascript?
Last modified: August 22, 2016

---

# Add Javascript?

 *  Resolved [dsb0328](https://wordpress.org/support/users/dsb0328/)
 * (@dsb0328)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/)
 * Hello. I am trying to figure out where to put existing Javascript from an existing
   site in to WordPress. Basically, I built a new site and used code from the original
   html site to build a client login area. That code works with some Javascript 
   in the <head> area of the existing site. Is there somewhere that I need to place
   this Javascript code in WordPress to make the client login work? Hopefully I 
   explained that correctly. Any help as soon as possible would be greatly appreciated.

Viewing 15 replies - 1 through 15 (of 29 total)

1 [2](https://wordpress.org/support/topic/add-javascript-1/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/add-javascript-1/page/2/?output_format=md)

 *  Thread Starter [dsb0328](https://wordpress.org/support/users/dsb0328/)
 * (@dsb0328)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664848)
 * Just t o be clear, I built a new site in WordPress to replace an existing site
   that was built in html. I got the code for the client login form (email and password
   and buttons) from the existing html and I need to put the Javascript that makes
   it work, from the head of the original site, somewhere in the WordPress site.
 *  [Brian](https://wordpress.org/support/users/briansteeleca/)
 * (@briansteeleca)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664851)
 * The proper way to add JavaScript to WordPress is with the `wp enqueue script`
   function in your theme’s functions.php file.
 * See the Codex for more info:
    [http://codex.wordpress.org/Function_Reference/wp_enqueue_script](http://codex.wordpress.org/Function_Reference/wp_enqueue_script)
 *  [Red Deer Web Design](https://wordpress.org/support/users/bdbolin/)
 * (@bdbolin)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664857)
 *     ```
       //Register Scripts
       	function theme_global_script() {
       		wp_enqueue_script("global-script", get_template_directory_uri()."/js/global_scripts.js", array('jquery'), true);
       	}
       	add_action( 'wp_enqueue_scripts', 'theme_global_script' );
       ```
   
 * Add that to the functions.php file of your theme, changing global_scripts.js 
   to your file. Note that the file path is /js/global_scripts.js
 * array(‘jquery’) will also auto load jQuery if needed. If not, remove it.
 *  Thread Starter [dsb0328](https://wordpress.org/support/users/dsb0328/)
 * (@dsb0328)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664861)
 * Thanks for the quick reply briansteeleca! I may be a little confused as there
   are so many different instructions on that page. Here’s the code that I want 
   to include in WordPress (altered with “example.js” and “example.com”. Also, not
   sure if I need the first <script LANGUAGE…> line.):
 * <script LANGUAGE=”JavaScript” SRC=”include/example.js”></script>
 * <script Language=”JavaScript”>
    function validateemail(sEmailAdd){ if (sEmailAdd
   == “”) { alert(“Please enter your email address and try again.”); document.frmLogin.
   txtusername.focus();
 *  return false;
    } else { return true; //var sFeatures = “top=” + ((screen.height/
   2) – 300) + “,left=” + ((screen.width / 2) – 400) + “,height=550,width=750,status
   =yes,toolbar=yes,location=yes,resizable=yes,directories=no,scrollbars=yes”; //
   window.open(“[http://www.example.com/timetrax/content/fmeTimeTrax.asp?Email=&#8221](http://www.example.com/timetrax/content/fmeTimeTrax.asp?Email=&#8221);
   + sEmailAdd,””,sFeatures); } }
 *  function CheckFields(frm) {
    if (frm.txtusername.value == “”) { alert(“Please
   enter your email address and try again.”); return false; }
 *  return true;
    }
 * </script>
 * Should I use this method in my Child Theme? If so, do I create a new file in 
   my child theme folder?:
 * Load a Script from a Child Theme without Dependencies
 * Register and enqueue the script in the same callback function with no dependencies,
   in the footer. See wp_register_script() for details. In this example, google_analytics_object.
   js is the Google Analytics tracking code (provided by Google) in a file.
 * <?php
    add_action( ‘wp_enqueue_scripts’, ‘child_add_scripts’ );
 * /**
    * Register and enqueue a script that does not depend on a JavaScript library.*/
   function child_add_scripts() { wp_register_script( ‘google-analytics’, get_stylesheet_directory_uri().‘/
   google_analytics_object.js’, false, ‘1.0’, true );
 *  wp_enqueue_script( ‘google-analytics’ );
    }
 * Or maybe use this? Also, where would I put it for my child theme?:
 * Using a Hook
 * Scripts and styles from a single action hook
 * /**
    * Proper way to enqueue scripts and styles */ function theme_name_scripts(){
   wp_enqueue_style( ‘style-name’, get_stylesheet_uri() ); wp_enqueue_script( ‘script-
   name’, get_template_directory_uri() . ‘/js/example.js’, array(), ‘1.0.0’, true);}
 * add_action( ‘wp_enqueue_scripts’, ‘theme_name_scripts’ );
 *  [tymax](https://wordpress.org/support/users/tymax/)
 * (@tymax)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664869)
 * Proper way as you have there at the end of ur post is to enqueue the script. 
   This make sure things like jQuery that some scripts rely on loads first, if u
   used jQuery and added the script to the header instead of enqueue it wouldn’t
   work.
 * Easiest was is to just and those scripts to the bottom of the footer. You can
   just use <script> tag.
 *  [Red Deer Web Design](https://wordpress.org/support/users/bdbolin/)
 * (@bdbolin)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664871)
 * dsb0328… if you read my post I gave you the exact code minus the file name that
   you have to replace. Copy paste and put into your functions.php file in the theme.
 * By the looks of it you will need to enque two files. The include/example.js and
   then create a file with the code you pasted as the second.
 *  Thread Starter [dsb0328](https://wordpress.org/support/users/dsb0328/)
 * (@dsb0328)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664875)
 * Thanks tymax. But I’m still confused. I think I’m going to have to do a lot more
   research on this, because I think I’m getting more confused as I go. Unless someone
   can assist me in where I put the “Hook” code and where I put the Javascript from
   the original website source code. I like to think that I’m okay in html and css,
   but when it comes to Javascript, I kinda fall all over myself.
 *  [Red Deer Web Design](https://wordpress.org/support/users/bdbolin/)
 * (@bdbolin)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664876)
 * Dude. The code I gave you is the “hook”
 * The javascript goes in it’s own new file from which you call in the hook.
 *  [Brian](https://wordpress.org/support/users/briansteeleca/)
 * (@briansteeleca)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664877)
 * Try this:
 * In your theme folder, create a js folder and put your javascript file in there:
   /
   wp-content/themes/your-theme-name/js/example.js
 * Then, in your functions.php file…
    /wp-content/themes/your-theme-name/functions.
   php … add this:
 *     ```
       /**
        * Proper way to enqueue scripts and styles
        */
       function your_theme_name_scripts() {
       	wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
       }
       add_action( 'wp_enqueue_scripts', 'your_theme_name_scripts' );
       ```
   
 * I this example, the script does not have any dependencies (such as jQuery) and
   the script will be added in the footer just below the closing body tag. If you
   need to load the script in the head, you can set `true` to `false`.
 * This is the proper way to load the JavaScript in WordPress rather than putting
   a <script> tag in your template.
 *  [Brian](https://wordpress.org/support/users/briansteeleca/)
 * (@briansteeleca)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664879)
 * You can also load more than one script if you need to:
 *     ```
       /**
        * Proper way to enqueue scripts and styles
        */
       function your_theme_name_scripts() {
       	wp_enqueue_script( 'script-name-01', get_template_directory_uri() . '/js/example01.js', array(), '1.0.0', true );
       	wp_enqueue_script( 'script-name-02', get_template_directory_uri() . '/js/example02.js', array(), '1.0.0', true );
       }
       add_action( 'wp_enqueue_scripts', 'your_theme_name_scripts' );
       ```
   
 * And if you need jQuery as a dependency, add it to the array() parameter as in
   Red Deer Web Design’s example.
 *  Thread Starter [dsb0328](https://wordpress.org/support/users/dsb0328/)
 * (@dsb0328)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664880)
 * Sorry Red Deer, I didn’t see your post. It must have come in while I was typing
   mine. Thank you all for the tremendous help. I am going to try it out and I’ll
   let you know if it works. Thank you. Thank you. Thank you.
 *  Thread Starter [dsb0328](https://wordpress.org/support/users/dsb0328/)
 * (@dsb0328)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664881)
 * Both posts actually, Red Deer. Again, sorry.
 *  [Red Deer Web Design](https://wordpress.org/support/users/bdbolin/)
 * (@bdbolin)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664882)
 * hah, weird. I was kinda wondering if they weren’t showing for some reason.
    Hopefully
   you get it working 🙂
 *  [WPyogi](https://wordpress.org/support/users/wpyogi/)
 * (@wpyogi)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664885)
 * Actually, they weren’t showing for a while – as they were caught in the spam 
   filter. The mods try to check and fish those out often but depending on time 
   of day, sometimes it’s a bit longer. :)!
 *  [Brian](https://wordpress.org/support/users/briansteeleca/)
 * (@briansteeleca)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/#post-5664887)
 * That explains a lot… Glad to know I’m not going crazy. ☺

Viewing 15 replies - 1 through 15 (of 29 total)

1 [2](https://wordpress.org/support/topic/add-javascript-1/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/add-javascript-1/page/2/?output_format=md)

The topic ‘Add Javascript?’ is closed to new replies.

## Tags

 * [head](https://wordpress.org/support/topic-tag/head/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [login](https://wordpress.org/support/topic-tag/login/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 29 replies
 * 5 participants
 * Last reply from: [dsb0328](https://wordpress.org/support/users/dsb0328/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/add-javascript-1/page/2/#post-5665047)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
