1

Trying to implement this jquery plugin http://www.myjqueryplugins.com/jquery-plugin/jrating i have trouble setting paths for stars in js file. its not possible to use php inside js file right? Cause I need to set those stars path to be inside webroot folder and i don't know how to do that without WWW_ROOT constant

(function($) {
    $.fn.jRating = function(op) {
        var defaults = {
            bigStarsPath : 'icons/stars.png'
            ...
Marko
  • 1,337
  • 5
  • 18
  • 37

3 Answers3

4

Just make it an absolute URL instead of a relative URL:

(function($) {
$.fn.jRating = function(op) {
    var defaults = {
        bigStarsPath : '/icons/stars.png'  //<-- notice the "/" before "icons"
        ...

That tells it to look for an 'icons' folder within the 'webroot' folder.

The same goes for any other files in the webroot. Sometimes you want to include css or javascript file that's within a library or something - in that case, you can include it by setting a "/" first - like this: /bootstrap/js/main.js.

Dave
  • 28,833
  • 23
  • 113
  • 183
1

There are many solutions:

  1. Copy this code to view file (.ctp) and use:

    bigStarsPath: '<?php echo $path_to_file; ?>'
    
  2. If You can add .js files to preprocessor of PHP: Parse a JavaScript file through PHP

  3. Set in view js variable and read in js.

Community
  • 1
  • 1
kicaj
  • 2,881
  • 5
  • 42
  • 68
1

in your header file put this code below script with set constant variable and use that JavaScript variable name called bipartisanship in your JS file, JS must be include after this code.

&lt;?php define('bipartisanship','/icons'); ?&gt;
    &lt;script type="text/javascript"&gt;
        var bipartisanship = '&lt;?php echo bipartisanship; ?&gt;'
    &lt;/script>
Badru
  • 41
  • 2