我可以更改脚本src并让javascript运行新代码吗? [英] can I change script src and have javascript run the new code?

查看:82
本文介绍了我可以更改脚本src并让javascript运行新代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实际上是演示文稿的页面,就像powerpoint一样。这是一个无限循环,自上次以来用ajax更新自己下载新的演示文稿内容。

I have a page that is actually a presentation, much like powerpoint. It is an infinite loop which updates itself with ajax to download new presentation content since last time around.

现在,当用户决定更改时,此后续行将会发生变化幻灯片中的过渡效果。

Now, this following line will be subject to change when user decides to change transitional effects in the slideshow.

<script id='transition' type='text/javascript' src='transitions/script/slide_off_to_right_fade.js'></script> 

但是,即使我用例如

<script id='transition' type='text/javascript' src='transitions/script/fade_in_out.js'></script> 

它仍然与最初加载文档时的情况相同。

It still does the same thing as was there when the document was initially loaded.

是否有一个特殊的函数/方法来调用来实现这个目的?非常感谢。

Is there a special function/method to call to achieve this? Many thanks.

Ps。跟踪幻灯片数量和每张幻灯片停留时间的数组也存在同样的问题 - 我更新了函数中的时间并运行了函数,但就好像我从未改变过一样......更多关于在另一篇文章...

Ps. The same problem with an array that keeps track of number of slides and the time to stay on each slide - I update the times in the function and run the function, but it is as if I never changed a thing... more about that in another post...

推荐答案

您可以动态创建脚本元素并将其插入DOM。如果您不使用jQuery,可以使用纯Javascript执行此操作,如下所示:

You can dynamically create a script element and insert it into the DOM. If you're not using jQuery, you can do this in pure Javascript as follows:

  var ref    = document.getElementsByTagName('script')[0],
      script = document.createElement('script');
  script.setAttribute('type', 'text/javascript');
  script.setAttribute('src',  'transitions/script/fade_in_out.js');
  script.setAttribute('id',   'newTransition');
  ref.parentNode.insertBefore(script, ref);

这篇关于我可以更改脚本src并让javascript运行新代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆