[wp-hackers] howto including full path in javascript file.
TobiasBg
wp-hackers at tobias.baethge.com
Mon Feb 8 20:32:56 UTC 2010
Hi,
> For
> instance I would like to call some images in my javascript file and I
> need to use a full path. What would be the best practice for this?
Besides the already presented ideas, I like to use the wp_localize_script function, like
wp_register_script( 'my-script', $javascript_url );
wp_localize_script( 'my-script', 'MY_SCRIPT', array(
'image_path' => plugin_dir_url(__FILE__),
'l10n_print_after' => 'try{convertEntities(MY_SCRIPT);}catch(e){};'
) );
wp_enqueue_scripts( 'my-script' );
This will enable you to use the MY_SCRIPT.image_path variable in your scripts. The advantage is, that you don't have to worry about the order of execution of the scripts, as WP will take care of that.
The wp_localize_script function was designed to make script files translateable, but as the file path is a string, too, there are no practical problems of using it.
Best wishes,
Tobias
More information about the wp-hackers
mailing list