• Hi all.
    I’m trying to use some javascript that will display the day, on just one of my pages. Through wordpress I have included this directly on the page:

    <script type="text/javascript" src="/scripts/happydate.js"></script>
    <script type="text/javascript">
    <!--
    happydate();
    //--></script>
    
    I created a file named happydate.js with the code below and put in a scripts folder under my childtheme directory (wp-content/themes/childtheme/scripts):
    
    <script type="text/javascript">
    
          <!--
          // Array of day names
          var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
                  "Thursday","Friday","Saturday");
          var now = new Date();
          document.write("Happy " + dayNames[now.getDay()] + ".");
          // -->
    
        </script>
    
    I also tried adding both the codes below (separately, at different times) to my functions.php file (in the same childtheme directory). Neither of them worked for me. 
    
    <?php
    function add_my_scripts(){
    wp_register_script(‘happydate’,
    get_bloginfo(‘stylesheet_directory’).’/js/happydate.js’, array(‘jquery’),’1.0′);
    wp_enqueue_script(‘happydate’);
    }
    add_action(‘init’, ‘add_my_scripts’);
    ?>
    
    < ?php
    if ( !is_admin() ) { // instruction to only load if it is not the admin area
       // register your script location, dependencies and version
       wp_register_script('happydate',
           get_bloginfo('stylesheet_directory') . '/scripts/happydate.js',
           array('jquery'),
           '1.0' );
    
       // enqueue the script
       wp_enqueue_script('happydate');
    }
    ?>

    [Please be sure to use the code buttons when posting code – as is, your code may have been corrupted here]

    I have no idea where in the steps above I went wrong, or if all the syntax is correct. Any insight is appreciated. I have a little bit of a grasp on this stuff, but I’m definitely a newbie. Be gentle!

    Thanks in advance.

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

The topic ‘Javascript in wordpress page, using child theme’ is closed to new replies.