I haven’t tested this, but you’ll probably get the gist of it anyway:
add_action('wp_enqueue_scripts', 'send_image_source_to_js');
function send_image_source_to_js() {
global $post;
// Does this post have a featured image? If yes, fetch the link / source
$has_image = $source = false;
if($thumb_id = get_post_thumbnail_id($post->ID)) {
$has_image = true;
$source_array = wp_get_attachment_image_src($thumb_id, array(300,300));
$source = $source_array[0];
}
// Build an array with what we've found about the post
$image_params = array(
'has_image' => $has_image ? 1 : 0,
'source' => $source ? $source : 0
);
// Register, localize and enqueue the script
wp_register_script('my_script', 'path/to/my_script.js');
wp_localize_script('my_script', 'image_params', $image_params);
wp_enqueue_style('my_script');
}
What wp_localize_script does is create a Javascript object which will be printed in your page’s html, which you can then use inside your JS code like this (I’m lazy, so I’ve added a non-sensical jQuery example):
$('#my_image').attr('src', image_params.source);
Let us know how it goes!