Setting Variable Path to Template Directory inside Script

If you want to send the url of the website, or other server side calculated values, to JavaScript you can use wp_localize_script (Codex Page). This function creates an object, with the values you’ve passed.

As the documentation states, you’ve got to include your script with wp_enqueue_script, else it doesn’t call localize_script. Also make sure to call wp_head() before your closing head tags, to make it work.