Kohana and the Javascript path
I have the following Kohana setup:
All my files go under "public_html / koh" My js files go under 'public_html / koh / media / js /'
I am using the html :: script helper to include those javascript files that generate the following html code for me:
<script type="text/javascript" src="/koh/media/js/site.js"></script>
In my js I am accessing one of the controllers like json / getsomething (which is http: // localhost / koh / json / getsomething ).
It works fine as long as I stay at the top of the controller: http: // localhost / koh / home
When I go to ' http: // localhost / koh / home / index , it displays the same page, but "json / getsomething" is not available from Javascript anymore.
How can I solve this problem?
Enable Javascript using absolute path? Create a variable in js like var fullPath = ' http: // localhost / koh / '?
What's the best practice?
Leonty
a source to share
How I did it.
I made a function url_base
to fit kohana url::base
and so it would switch when I went from localhost to production.
Show template:
<script type="text/javascript">
function url_base() { return "<?php echo url::base();?>"; }
</script>
And then in config.php:
if(IN_PRODUCTION) {
$config['site_domain'] = '/';
}
else {
//if in localhost redirect to localhost/mysite
//instead of just localhost
$config['site_domain'] = '/mysite/';
}
a source to share
(A bit late, but hopefully still helpful). Another way I use to better organize my js-serverside vars.
I put some basic variables somewhere - eg. in the "before ()" function:
$this->template->appconf = array(
'url_base' => url::base(),
'l' => substr(I18n::$lang, 0, 2),
);
Now I can add whenever I need an additional variable:
$this->template->appconf['page_key'] = 'product_page';
And finally, in the template, this purity:
<script type="text/javascript">
var appconf = <?php echo json_encode($appconf); ?>;
</script>
Use this:
<script type="text/javascript">
console.log(appconf.url_base); // "/mysite/"
</script>
a source to share