Title: Need Help Calling Javascript Function
Last modified: August 19, 2016

---

# Need Help Calling Javascript Function

 *  [moessap](https://wordpress.org/support/users/moessap/)
 * (@moessap)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/)
 * I found a javascript code I’d really like to use, but can’t find out how to implement
   it.
 * I don’t know anything really about javascript, but from what I understand, you
   create some javascript function which goes in the <head> section of the html 
   code, like this:
 * <script language=”javascript”>
    “function” </script>
 * Then you call the function in the body of the html file to use it. I’m not sure
   how to use it with how the blog is set up though. I found a folder titled “scripts”
   in my theme folder, so I created the script and uploaded it there, but this doesn’t
   work. I also tried putting the script in “page.php”, but this doesn’t work either.
 * Anyone have any ideas?
 * Thanks… I appreciate any help :)!

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

 *  [cdybenko](https://wordpress.org/support/users/cdybenko/)
 * (@cdybenko)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1121876)
 * Try linking to the javascript file in the head of your page.php document
 *     ```
       <html>
       <head>
       <script type="text/javascript" src="<yoursite>/wp-content/themes/theme_name/scripts/yourjavascript.js"></script>
   
       </head>
       <body>
       </body>
       </html>
       ```
   
 * Then try calling the javascript functions from anywhere in your page.php file.
 * Also try a theme that uses javascript and see how they link to the script source
   and then use it in the theme.
 * Good luck!
 *  Thread Starter [moessap](https://wordpress.org/support/users/moessap/)
 * (@moessap)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1121881)
 * Hmmm ok… thanks a lot for the help :)!
 *  [cdybenko](https://wordpress.org/support/users/cdybenko/)
 * (@cdybenko)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1121884)
 * I hope I didn’t make it sound too confusing.
 * Sometimes it’s easiest to learn by downloading a theme that uses javascript and
   seeing how they link to the javacript source file and call it from their php 
   files.
 * There are lots of introduction to javascript tutorials online if you google for
   them.
 *  Thread Starter [moessap](https://wordpress.org/support/users/moessap/)
 * (@moessap)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1121886)
 * I’m a little confused actually, lol… but I usually figure things out through 
   trial and error… and google ya.
 * Thanks again :).
 *  [rajanpanicker](https://wordpress.org/support/users/rajanpanicker/)
 * (@rajanpanicker)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1122187)
 * My theme dont have a scripts directory.But it does have a js directory.I believe
   I should be copying the files into that cricket.
    And could you make it a bit
   more specific as my single.php is where I need to display my javascript code.
   Here my single.php has elements that start with
 * <?php get_header(); ?>
    <?php global $options; ………………..
 * So here what and where shall I add my .js function call.
    Also my webhost has
   a htdocs and inside that lies all the themes and other things.So what should 
   be the path? <script type=”text/javascript” src=”<www.mysite.com/>confusedpath/
   yourjavascript.js”></script> Thanks in advance
 *  [Justin Tadlock](https://wordpress.org/support/users/greenshady/)
 * (@greenshady)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1122188)
 * Please never add scripts to your theme’s header file. WordPress has a function
   for this: `wp_enqueue_script()`. Assuming your script is in your theme’s `/js`
   folder, add this code to your theme’s `functions.php`:
 *     ```
       add_action( 'template_redirect', 'load_my_js' );
   
       function load_my_js() {
   
       	/* Only load for single posts. */
       	if ( is_single() )
       		wp_enqueue_script( 'my-js', get_stylesheet_directory_uri() . '/js/yourjavascript.js', false, 0.1 );
       }
       ```
   
 *  [rajanpanicker](https://wordpress.org/support/users/rajanpanicker/)
 * (@rajanpanicker)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1122189)
 * What if I want to display the script only in my posts and not in pages and home
   page?Then what I should I do?I tried inserting in single.php but nothing is happening.
   So if I include the above script in functions.php,it will be displayed on all
   my pages and posts right.I dont want that,I want it to be displayed only on my
   posts.Anyway thanks in advance.
 *  [Justin Tadlock](https://wordpress.org/support/users/greenshady/)
 * (@greenshady)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1122190)
 * The code I just posted does that.
 *  [rajanpanicker](https://wordpress.org/support/users/rajanpanicker/)
 * (@rajanpanicker)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1122191)
 * No its not working.I coped the script in js directory,then copied this code randomly
   inside functions.php but still its of no use.Or should I name the path instead
   of /js/yourjavascript.js
    if I have a htdocs,that must mean that I should incude
   that also in the path right?
 *  [magickman5](https://wordpress.org/support/users/magickman5/)
 * (@magickman5)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1122192)
 * hi rajanpanicker,
 * just follow this link
 * [Want to know more ….](http://codex.wordpress.org/Function_Reference/wp_enqueue_script)

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

The topic ‘Need Help Calling Javascript Function’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 5 participants
 * Last reply from: [magickman5](https://wordpress.org/support/users/magickman5/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/need-help-calling-javascript-function/#post-1122192)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
