Javascript 用ajax更新js函数定义,如何重新定义函数/eval?
我有一个slideshow web应用程序,对于某些幻灯片,它包括一个beforeSlideshowAction,它与该幻灯片一起从服务器端呈现,javascript与该幻灯片的内容一起呈现,因为它是生成该内容的幻灯片模板的一部分 但是,下面的javascript函数不会与我的内容一起更新。例如,如果我要加载幻灯片,然后在这两张幻灯片上切换位置(在幻灯片放映运行一个完整的周期后,将由ajax进行更新),下面的beforeSlideshowAction_slide1()现在将呈现为beforeSlideshowAction_slide2(),但调用时,将只存在一个名为beforeSlideshowAction_slide1()的函数,我假设所有javascript函数都只在页面初始加载时注册 我记得有一次我在包含更新函数的脚本块上调用eval解决了一个类似的问题,我的问题有没有一个更简单的解决方案Javascript 用ajax更新js函数定义,如何重新定义函数/eval?,javascript,jquery,twig,Javascript,Jquery,Twig,我有一个slideshow web应用程序,对于某些幻灯片,它包括一个beforeSlideshowAction,它与该幻灯片一起从服务器端呈现,javascript与该幻灯片的内容一起呈现,因为它是生成该内容的幻灯片模板的一部分 但是,下面的javascript函数不会与我的内容一起更新。例如,如果我要加载幻灯片,然后在这两张幻灯片上切换位置(在幻灯片放映运行一个完整的周期后,将由ajax进行更新),下面的beforeSlideshowAction_slide1()现在将呈现为beforeSl
{# 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>
{#注释:从这里,文档将由Ajax更新}
{#注释:ordernumber现在是1}
//这里有一些内容
函数beforeSlideshowAction_slide{{ordernumber}}(){
//为当前幻灯片的视频内容指定适当的值
videoContent[{ordernumber}}]='video={{{data0}}&高度={{{data1}}&宽度={{data2}}&编解码器={{{data3}}&全屏={{{data5}';
}
{#注释:ordernumber现在是2}
//这里有一些内容
//幻灯片2没有“beforeSlideshowAction”功能
不要担心我可能会调用一个不存在的函数,我在一个try/catch块中调用它,我已经对它进行了设置,以便为try/catch中的每张幻灯片调用beforeslideshow函数,在幻灯片模板中,如果我想要一个函数,我就在那里定义一个函数,如果不需要,我就跳过它。下面是我最终解决它的方法
<script id="beforeSlideshowAction{{ ordernumber }}_script" type='text/javascript'>
//define the function for later use (page updates)
window.beforeSlideshowAction_slide{{ ordernumber }} = function() {
console.log('runnin beforeSlideshowAction for slide {{ ordernumber }}');
//assign appropriate value to videoContent for current slide
videoContent[{{ ordernumber }}]='video={{ data0 }}&height={{ data1 }}&width={{ data2 }}&codec={{ data3 }}&fullscreen={{ data5 }}';
}
//run
var x = window['beforeSlideshowAction_slide{{ ordernumber }}'];
x();
</script>
//定义函数供以后使用(页面更新)
window.beforeSlideshowAction_slide{{ordernumber}}=function(){
log('runnin beforeSlideshowAction for slide{{ordernumber}}');
//为当前幻灯片的视频内容指定适当的值
videoContent[{ordernumber}}]='video={{{data0}}&高度={{{data1}}&宽度={{data2}}&编解码器={{{data3}}&全屏={{{data5}';
}
//跑
var x=window['beforeSlideshowAction_slide{{{ordernumber}}'];
x();
…稍后,当ajax改变了这一点,而slide1函数可能会改为slide3函数时
//replace the innerHTML of body with the new stuff
document.getElementById('slideshow_body').innerHTML=newContent;
//update the videoContents and run other beforeSlideshow-actions
for (var i=1;i<(pauseTimes.length+1);i++)
{
try {
console.log('slide '+i+' action...');
//set previous version of beforeSlideshowAction i to null
window['beforeSlideshowAction_slide'+i] = null;
//define it anew (will this run it too?)
ie_compatible_eval( document.getElementById('beforeSlideshowAction'+i+'_script').innerHTML )
}
catch(e) {
console.log('slide '+i+' none');
//do nothing since we will not have a function named that for every slide, error is expected
}
}
//用新内容替换body的innerHTML
document.getElementById('slideshow_body')。innerHTML=newContent;
//更新视频内容并在幻灯片放映之前运行其他操作
对于(var i=1;i