We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23044
    • 20 Posts
    I’m trying to insert the following javascript into the head of a document with the $modx->regClientStartupScript(); function. I’ve got a few of these calls in a snippet to add css and js files into a single document. Others work. This one doesn’t.

    $readyFunction = "<script type='text/javascript'>
    //<![CDATA[      
                $(document).ready(function() {
                    $('#container-1').tabs();
                    $('#container-2').tabs({on: 2});
                    $('#container-3').tabs({fxSlide: true});
                    $('#container-4').tabs({fxFade: true, fxSpeed: 'fast'});
                    $('#container-5').tabs({fxSlide: true, fxFade: true, fxSpeed: 'fast'});
                    $('#container-6').tabs({fxFade: true, fxSpeed: 'fast', callback: function(newTab, oldTab) {
                        alert('New tab: ' + newTab.id + ', old tab: ' + oldTab.id);
                    }});
                    $('#container-7').tabs({fxAutoheight: true});
                    $('#trigger-tab').after('<p><a href="#" onclick="$(\'#container-1\').triggerTab(3); return false;">Activate third tab</a></p>');
                    $('#custom-tab-by-hash').title('New window').click(function() {
                        var win = window.open(this.href, '', 'directories,location,menubar,resizable,scrollbars,status,toolbar');
                        win.focus();
                    });
                });//]]>
    </script>";
    $modx->regClientStartupScript($readyFunction);
    


    I get
    Parse error: syntax error, unexpected '$' in /manager/includes/document.parser.class.inc.php(705) : eval()'d code on line 20
    (ignore line number. code above is cut from snippet)

    I was operating on the assumption that anything inside the CDATA tags would not be parsed. I guess I was wrong. How can I tell the document parser class to simply mind it’s own business and pay no attention to anything between CDATA tags?
      • 10487 MODX Staff
      • 1,535 Posts
      Hi perlmonger,

      Check this line:
      $('#trigger-tab').after('<p><a href="#" onclick="$(\'#container-1\').triggerTab(3); return false;">Activate third tab</a></p>');

      You need to escape the doublequotes - I believe these are causing your error.
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 23044
        • 20 Posts
        Quote from: garryn at Sep 19, 2006, 03:56 PM

        Hi perlmonger,

        Check this line:
        $('#trigger-tab').after('<p><a href="#" onclick="$(\'#container-1\').triggerTab(3); return false;">Activate third tab</a></p>');

        You need to escape the doublequotes - I believe these are causing your error.

        #%*^&#()! I’ve been looking at that for about an hour and searching the web for another. I didn’t see it. Thanks for the extra eyes. It works every time. I need to do some experimentation with this in regards to escaping double quotes and single vs double quotes and find a pattern that works. Any tips or gotcha’s to look out for with parsing code this way?
          • 22815
          • 1,097 Posts
          Quote from: perlmonger at Sep 19, 2006, 03:46 PM

          I was operating on the assumption that anything inside the CDATA tags would not be parsed. I guess I was wrong. How can I tell the document parser class to simply mind it’s own business and pay no attention to anything between CDATA tags?
          The document parser is not an XML parser. You *do* want the MODx parser to parse this text, because it’s part of the text you’re trying to put into the head of your XHTML document.

          And I believe the parse error you are getting is not from the MODx parser but the PHP parser.

          Personally, I’d use HEREDOC format on this.

          EDIT: If you ever have a big chunk of code with ’ and " in it that you want to send to a variable in PHP, use heredoc:
          http://uk2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

          $readyFunction = <<<EOD <script type='text/javascript'>
          //<![CDATA[    
          all that stuff
          EOD;
          

            No, I don&#39;t know what OpenGeek&#39;s saying half the time either.
            MODx Documentation: The Wiki | My Wiki contributions | Main MODx Documentation
            Forum: Where to post threads about add-ons | Forum Rules
            Like MODx? donate (and/or share your resources)
            Like me? See my Amazon wishlist
            MODx "Most Promising CMS" - so appropriate!
            • 23044
            • 20 Posts
            OK, good point. I’m getting sidetracked here. I’ll do some reading on HEREDOC in php. I’m guessing it’s the same idea as Perl so I just need to get the syntax. I need to be able to insert the css and javascript as intended without having to change it in any way to parse it so I think you are absolutely right about HEREDOC being the best way to go.

            Thanks much