We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9889
    • 7 Posts
    Wondering if it is any way possible to have a TV parsed in an external (linked) javascript file such as:

    TV:

    [[!*top-img?]]’ - TV for image file link

    JS:

    <script type="text/javascript">

    $(function() {
    var img = new Image();
    $(img).load(function() { // when image has loaded...
    $(this).css(’display’, ’none’); // hide image by default
    $(’#loader’).removeClass(’loading’).append(this); // remove ’div#loader.loading’ class (loading anim gif), then insert our image in ’loader’ div
    $(this).fadeIn(700); // fade image in
    }).attr(’src’, ’[[!*top-img?]]’);
    });

    </script>
      • 33337
      • 3,975 Posts
      Make that JS a resource in MODX?
        Zaigham R - MODX Professional | Skype | Email | Twitter

        Digging the interwebs for #MODX gems and bringing it to you. modx.link
        • 9889
        • 7 Posts
        How would I do that? Make the JS a resource??

        Or could I just maybe create a Snippet with JS code included?

        Which one would load faster??
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You could use a small bit of javascript code in the template head to set it as a global JS variable, and use the TV there.
          <script type="text/javascript">
          myvar = '[[*top-img]]';
          </script>

          http://snook.ca/archives/javascript/global_variable
            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
            • 9889
            • 7 Posts
            Great - thanks Susan!

            Also, would you recommend inbedding JS int a Snippet and running it from there or is that sort of like putting horse before the cart?

            A very beginning "programmer" here ... (;
              • 9102 ☆ A M B ☆
              • 318 Posts
              You wouldn’t put JS in a snippet, those are for PHP code, but you could put it in a chunk or a template.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Keep in mind that PHP code (snippets) are run in the back-end while creating the page to be served. Javascript is run in the browser. It is quite possible to have a snippet generate output that includes javascript code, especially using the $modx->regClientStartupScript() which loads <script>...</script> tags in the HTML head, and $modx->regClientScript() which loads the tags just before the final </body> tag. I use this method a lot to load the required javascript for galleries, forms and anything else that will only be relevant to one or a few pages so that the pages that don’t use it don’t have the overhead of loading the javascript.
                  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
                  • 9889
                  • 7 Posts
                  Hmm yes, I saw documentation for both of these functions and though about using them as well ... Thanks again for the help!