我可以修改脚本src并让javascript运行新代码吗?

时间:2022-03-01 16:58:59

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.

我有一个页面,它实际上是一个演示文稿,很像powerpoint。它是一个无限大的循环,使用ajax更新自己,以便从上一段时间开始下载新的表示内容。

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> 

However, even though I replace this entire tag with for example

但是,即使我用例如替换整个标记

<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. 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...

同样的问题也存在于一个数组中,这个数组记录了幻灯片的数量和每个幻灯片上的时间——我更新了函数中的时间并运行了函数,但是好像我从来没有改变过什么……在另一篇文章中有更多关于这一点的信息……

2 个解决方案

#1


4  

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:

您可以动态创建一个脚本元素并将其插入到DOM中。如果不使用jQuery,可以使用纯Javascript完成如下操作:

  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);

#2


2  

Creating a new <script> element and adding it to the DOM tree should work. If you are using a JavaScript library like jQuery you can use the convenient getScript() function.

创建一个新的

#1


4  

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:

您可以动态创建一个脚本元素并将其插入到DOM中。如果不使用jQuery,可以使用纯Javascript完成如下操作:

  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);

#2


2  

Creating a new <script> element and adding it to the DOM tree should work. If you are using a JavaScript library like jQuery you can use the convenient getScript() function.

创建一个新的