2

I'm having a shot at developing my first plugin for Joomla, its just a simple plugin which places some javascript just before the close of the body. The javascript works fine if I place it directly in the index.php for the template so Im confident in the js. My plugin however is throwing an error

Parse error: syntax error, unexpected 's' (T_STRING) in C:\xampp\blah\blah\plugins\system\reftagger\reftagger.php on line 50

I think it's to do with the use of the double quotes on the line 49 (where its adding the g.src) which is throwing things out of whack. The snippet below shows the script that Im trying to add which includes the double quotes

 $ReftaggerOut = "
            <script type='text/javascript'>
                var refTagger = {
                    settings: {
                        bibleVersion: ".$BibleTranslation."            
                    }
                };
                (function(d, t) {
                    var g = d.createElement(t), s = d.getElementsByTagName(t)[0];
                    g.src = "//api.reftagger.com/v2/RefTagger.js";
                    s.parentNode.insertBefore(g, s);
                }(document, "script"));

            </script>

    ";      

I've attempted

  1. to change the g.src to single brackets
  2. make it a /" for the g.src
4

It is the double quotes in the script/string. Try this:

<?php
$ReftaggerOut = "
    <script type='text/javascript'>
        var refTagger = {
            settings: {
            bibleVersion: ".$BibleTranslation."            
            }
        };
        (function(d, t) {
            var g = d.createElement(t), s = d.getElementsByTagName(t)[0];
            g.src = '//api.reftagger.com/v2/RefTagger.js';
            s.parentNode.insertBefore(g, s);
        }(document, 'script'));
    </script>
";
?>

Or if you want the double quotes, escapes them like this:

$ReftaggerOut = "
    <script type='text/javascript'>
        var refTagger = {
            settings: {
            bibleVersion: ".$BibleTranslation."            
            }
        };
        (function(d, t) {
            var g = d.createElement(t), s = d.getElementsByTagName(t)[0];
            g.src = \"//api.reftagger.com/v2/RefTagger.js\";
            s.parentNode.insertBefore(g, s);
        }(document, \"script\"));
    </script>
";
  • THanks GDP thats done the job, thought I'd tried that but obviously missed something! – rowanwins May 4 '14 at 13:00
  • NP...I'm constantly doing the same thing....if you don't mind spending a few bucks, the best PHP editor out there is PhpEd by Nusphere at nusphere.com/products/phped.htm . It was invaluable to me in learning the language, and most especially the syntax. – GDP May 4 '14 at 13:05
3

I realize you've solved the syntax error issue, but the right way to solve this problem is to not insert javascript with PHP this way.

What you should be doing is:

1) create your javascript file in media/plg_yourplugin/js/your-js-file.js

2) in PHP use JHTML library API to add the javascript file to the index.php area.

Here's a blog post by @Michael on how to do this properly: http://www.babdev.com/blog/139-use-the-media-folder-allow-overridable-media

  • Thanks for the suggestion Chad, I'll have a shot using that approach although it might be a bit beyond me! – rowanwins May 5 '14 at 4:13
0

You have a PHP parsing error.

If your string starts with " and ends with ". All internal " must be \".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.