Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #719422

    Hi,

    I need to add a script element (<script src="js/my.js"></script>) to the <head> of an individual page (my.html) of my site.

    I don’t want this in the head of every page – just my.html.

    What would be your recommended (child theme) approach for this?

    Many Thanks.

    #719425

    Hi!

    Yes, we definitely recommend child theme otherwise your changes will be overwritten with each update – http://kriesi.at/documentation/enfold/using-a-child-theme/

    Then add my.js file into js folder in your child theme and then add following code to Functions.php file of your child theme

    function my_custom_script() {
    if(is_page(59)){
        wp_enqueue_script( 'my-js', get_stylesheet_directory_uri() . '/js/my.js', array(), '1.0.0', true );
    }
    }
    add_action( 'wp_enqueue_scripts', 'my_custom_script' );

    then replace 59 with the ID of your page

    Regards,
    Yigit

    #719432

    Thanks Yigit – seems I also need to add <body onload="myScript()"> of this page. Any way to do this with a function?

    #719746

    Hi,

    Please add following code to Functions.php file as well

    function add_body_attr(){
    ?>
     <script>
    jQuery(window).load(function(){
    jQuery('body.page-id-59').attr('onload','myScript()');
    });
     </script>
    <?php
    }
    add_action('wp_footer', 'add_body_attr');

    Best regards,
    Yigit

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.