Posted by starbow on February 2, 2007 at 12:25am
I trying to figure out the best way of getting base_path() into my javascript. I wrote some dynamic form updating code kinda like this:
Citris.new_info_submit = function() {
$(this).attr( "style", "cursor:wait");
var params = $('#node-form').formToArray( true ); // collect the form's parameters
$.post( 'mypath', params, function(data) {
$('#citris_info_wrapper').html( data );
$('#citris_info').highlightFade( 'yellow' );
Citris.citrisAttach(); // reattach events to new html
$(this).attr( "style", "cursor:auto");
} );
return false;
} which worked great, until I used it in a test environment that ran in a subdirectory. So I changed the above to be:
...
uri = Citris.basePath + "mypath";
$.post( uri, params, function(data) {
...And add this to my page.tpl.php
<script type="text/javascript">
$(document).ready(function(){
Citris.basePath = <?php print base_path() ?>;
});
</script> Which works, but feel like a big-old hack. Does anyone know a cleaner way to do this?
Thanks,
-tao

Comments
See Javascript Tools module
We pass the base_path there. This sort of thing should be passed through
drupal_add_js()of type 'setting'.Thanks!
Thanks, that was exactly the info I wanted.
For those playing along at home, I removed the code from the page.tpl.php, added
drupal_add_js(array( 'citris' => array( 'basePath' => base_path(), ), ), 'setting');to my module, and changed my javascript to:
uri = Drupal.settings.citris.basePath + "mypath";Much cleaner and works great.