add jquery file if a certain page is included

I was wondering if anyone could help me by explaining if it is possible to enqueue a javascript file only if for example homepage.php is included.

what i have tried to do inside homepage.php:

class homepage_js {
static $add_script;

static function init() {
    add_action('init', array(__CLASS__, 'register_script'));
    add_action('wp_footer', array(__CLASS__, 'print_script'));
}

static function register_script() {
    wp_register_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
}

static function print_script() {
    if ( ! self::$add_script )
        return;

    wp_print_scripts('my-script');
}
}

homepage_js::init();

Thanks in advance for help.

Solutions

If this is for your wordpress home page you can do this in functions.php

add_action( 'wp_enqueue_scripts' , my_enqueue_scripts );
function my_enqueue_scripts(){
    if ( is_home() ) {
        wp_enqueue_script( ... ); 
    }
}

Similar questions

How to use jquery included with wordpress for jpanelmenu
I have successfully been using jpanelmenu in my wordpress themes for some time, loading jquery from the google api, like this: I'd rather use the version of jquery that comes included with wordpress, but can't figure out how to make that happen. Can anyone advise me on how to do that, or tell me if I'm wrong in thinking that using wp's built-in jqu...
Is jQuery included in WordPress by default?
When I downloaded the latest version 3.8.1, inside wp-include/js there is a jQuery folder. Can I assume that WordPress will always include it, meaning I don't have to make my own call? E.g.
Wordpress head: After a manual including of jQuery, it is included again by a plugin... How to solve?
Well, the title is pretty explanatory... I am building a custom theme where all the fancy jQuery stuff (sliders, galleries, toggle, tooltips, etc) are manually added to head of the document, and of course, I call jQuery manually too. I prefer to work on this way instead of running wordpress plugins. But the problem is... There is one single plugin ...
jQuery plugin is undefined although is properly included before my js code
I am working on this quite strange issue for several days now, and I really don't understand why that happens. Basically, the environment is WordPress and I am including two js files: vendor.js -> generated by gulp and all it does is concatenating bower libs together myScript.js -> my code What I know (with proof): BUT even if everything looks fine...
WordPress / NPM error - jQuery must be included before Bootstrap's JavaScript
I'm using NPM to bundle bootstrap and several other scripts for a Wordpress theme. By default, WordPress loads jquery, so I have excluded jquery from the bundle. In google dev tools can see that jquery is being loaded before my bundled JS file but I still get this error. pgthrottle.min.js:2 Uncaught TypeError: Bootstrap's JavaScript requires jQuery...
Is comment metadata included in the export file?
If I would export a WordPress database, posts and comments etc. are saved to an XML file. Would comment metadata be included in this XML file? I.e. the table wp_commentmeta, mentioned here: http://codex.wordpress.org/Database_Description#Table:_wp_commentmeta. Here: The WordPress eXtended Rss (WXR) Export/Import, XML Document Format Decoded...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.