
<script type="text/javascript"> var myThemePath = '<?php echo $this->getThemePath(); ?>'; </script>
Then the variable will be available to javascript on your page. For example:
$('p.thing').append('<img src="' + myThemePath + '/images/thing.png" class="stuff" />');
Note that you can't output php variables like this in the javascript files themselves -- but if you set the variable in your theme template, it will be available to all javascript loaded by the page (just make sure you use a variable name that is unique enough that it won't accidentally overwrite some other variable being used by some javascript).
I have a script.js file in my themes js folder (e.g. js/script.js) that contains:
Modernizr.load({ test: Modernizr.mq('only all and (max-width: 400px)'), nope: 'http://www.website.co.uk/themes/mytheme/js/respond.js' });
Which works fine but I'd rather have a relative url there. If I try:
Modernizr.load({ test: Modernizr.mq('only all and (max-width: 400px)'), nope: '<?php echo $this->getThemePath(); ?>/js/respond.js' });
But no joy. Tried adding the variable as suggested by Jordan too but no good.
the respond.js file live in the same folder as the script calling it - shouldn't I just be able to use something like this:
Modernizr.load({ test: Modernizr.mq('only all and (max-width: 400px)'), nope: 'respond.js' });
Anyone shed any light on what I'm doing wrong?
And you can't just use a relative path because the path is relative to the url that the user is browsing, which is never the same as the actual location of the files (because C5, as will all CMS's and frameworks, is really serving everything from index.php -- all of your theme files etc. are loaded by the php code on the server and sent to the browser at a different location than where the files truly exist).
Modernizr.load({ test: Modernizr.mq('only all and (max-width: 400px)'), nope: '/js/respond.js' });
And place any scripts like respond.js that could be used cross-theme in the root 'js' folder. As there is nothing in the root js folder - I presume that is the intention of it?
Alternatively, you can use a semi-relative link in theme js files by doing something like this:
Modernizr.load({ test: Modernizr.mq('only all and (max-width: 400px)'), nope: '/themes/*your_theme*/js/respond.js' });