I have a slideshow web application that for some slides include a beforeSlideshowAction that is rendered from serverside together with that slide, the javascript is rendered together with the contents of that slide since it is part of a slide-template generating this content.
However, the javascript functions as of below is not updated together with my content. For example, if I were to load the slideshow, then switch places on these two slides (which update will be done by ajax after the slideshow has ran a full cycle), the beforeSlideshowAction_slide1() as of below will now be rendered as beforeSlideshowAction_slide2(), but when called, there will only exist a function called beforeSlideshowAction_slide1(), I assume all javascript function are registered upon page initial load only.
I remember solving a similar issue another time by calling eval on the script block containing the updated function, is there a less hackish solution to my problem?
{# comment: from here the document will be updated by Ajax #}
<div id="slideshowbody">
{# comment: ordernumber is now 1 #}
<div id="slide{{ ordernumber }}">
//some content goes here
<script>
function beforeSlideshowAction_slide{{ ordernumber }}() {
//assign appropriate value to videoContent for current slide
videoContent[{{ ordernumber }}]='video={{ data0 }}&height={{ data1 }}&width={{ data2 }}&codec={{ data3 }}&fullscreen={{ data5 }}';
}
</script>
</div>
{# comment: ordernumber is now 2 #}
<div id="slide{{ ordernumber }}">
//some content goes here
//slide 2 doesn't have a function for a "beforeSlideshowAction"
</div>
</div>
Don’t worry that I might be calling a function that doesn’t exist, I do it in a try/catch block, I have set it up so that a beforeSlideshowFunction is called for each slide in a try/catch, and in the slide-template I just define a function there if I want one, if not I just skip it.
Here is how I ended up solving it
…and later on, when this has been changed by ajax and the slide1 function might instead be the slide3-function…