We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3677
    • 130 Posts
    Can anyone help with a mini-tutorial on how to accomplish passing a tv value from a resource to a javascript file loaded by that resource? I actually have my javascript set up as it's own modx resource so that it can reference context settings inline, but it can not access tvs that are set on the resource that called it.

    This is the closest info I have found while searching: https://www.augmentblu.co.uk/blog-it/modx-tutorials/2012/04/30/passing-modx-variables-to-javascript/
    It is tailored to mootools, and I am looking for help with a jQuery environment.

    -Brian

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can use something like
      <script href="myscript.js?resource=[[*id]]"></script>

      in your calling resource, then in the .js resource have a snippet to get the $_REQUEST['resource'] value, then return the TV value for that resource ID.

      Do make sure to validate the REQUEST or GET before you use it for anything! [ed. note: sottwell last edited this post 12 years, 8 months ago.]
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        You can also just put placeholders or variables in the JS code and either replace them explicitly in the snippet, put the JS code in a chunk and get it with $modx->getChunk('chunkName', $fields) or just set the placeholders in the snippet. The JS code could also be in a file with placeholders. These are off the top of my head, so they may not be exactly right, but they should give you the idea.


        Method 1 - For a simple case:

        $var1 = 'someValue';
        $var2 = 'someotherValue';
        
        $jsCode = '
        <script type="text/javascript">
        function whatever() {
           var1 = [[+var1]];
           var2 = [[+var2]];
           /* more code here */
        }
        </script>';
        
        $jsCode = str_replace('[[+var1]]', $var1, $jsCode);
        $jsCode = str_replace('[[+var2]]', $var2, $jsCode);
        
        $modx->regClientStartupScript($jsCode);
        


        method 2 - For more elaborate code:

        jsChunk:
        <script type="text/javascript">
        function whatever() {
           var1 = [[+var1]];
           var2 = [[+var2]];
           /* more code here */
        };
        </script>
        

        Snippet code:
        $fields = array(
          'var1' = 'someValue',
          'var2' = 'someOtherValue',
        );
        
        $jsCode = $modx->getChunk('jsChunk', $fields);
        $modx->regClientStartupScript($jsCode);
        


        method 3 - using a file

        jsfile.js

        function whatever() {
           var1 = [[+var1]];
           var2 = [[+var2]];
           /* more code here */
        };
        


        Snippet code:
        $fields = array(
          'var1' = 'someValue',
          'var2' = 'someOtherValue',
        );
        
        $modx->setPlaceholders($fields);
        $modx->regClientStartupScript('jsfile.js');
        









          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting