I'm working on a template in Craft 2, and I have a number of divs which are part of a Javascript "read more" script, which automatically shortens the visible text at a set number of characters and then adds a "read more" link. I have added the Javascript in the page template, but the entire text of the div
displays briefly before the page loads completely. I have a Javascript to hide the text until page load (below), but the text is still briefly visible. It was successful in my non-Craft test, however, with the text never visible on page load. In the meantime, I've had to resort to a seemingly redundant default style of display:none
style for .post
. With that style in place, the page does load correctly.
Is this a problem having to do with positioning within the template?
I've tried to position the Javascripts above and below {% extends "_layout" %}
to no effect.
{% set myJs4 %}
// Set the display property of the element to none using CSS
var divsToHide = document.getElementsByClassName("post");
//divsToHide is an array
for(var i = 0; i < divsToHide.length; i++){
divsToHide[i].style.display = "none";
}
// Remove the display property after the page loads using the DOMContentLoaded event
document.addEventListener("DOMContentLoaded", function() {
var divsToShow = document.getElementsByClassName("post");
//divsToShow is an array
for(var i = 0; i < divsToShow.length; i++){
divsToShow[i].style.display = "block";
}
});
{%endset%}
{% includeJs myJs4 %}
And then,