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>
Make that JS a resource in MODX?
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??
-
☆ 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
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 ... (;
-
☆ 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.
-
☆ 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.
Hmm yes, I saw documentation for both of these functions and though about using them as well ... Thanks again for the help!