如何强制脚本重新加载并重新执行? [英] How to force a script reload and re-execute?

查看:191
本文介绍了如何强制脚本重新加载并重新执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从第三方加载脚本的页面(新闻提要)。

脚本的 src  < div id =div1287> 
<! - 动态生成的元素将在此处显示。 - >
< / div>

< script id =script0348710783type =javascript / text>
< / script>

< script type =javascript / text>
document.getElementById('script0348710783').src ='http:// oneBigHairyURL';
< / script>

http:// oneBigHairyURL 然后用新闻提要中的各种东西创建和加载元素,格式很漂亮,等等< div1287 (Iddiv1287被传入 http:// oneBigHairyURL ,这样脚本知道在哪里加载内容)。

唯一的问题是,它只加载一次。我希望它每n秒重新加载一次(因此显示新内容)。



所以,我想我会试试这个:

 < div id =div1287> 
<! - 动态生成的元素将在此处显示。 - >
< / div>

< script id =script0348710783type =javascript / text>
< / script>

< script type =javascript / text>
loadItUp = function(){
alert('loading ...');
var divElement = document.getElementById('div1287');
var scrElement = document.getElementById('script0348710783');

divElement.innerHTML ='';
scrElement.innerHTML ='';
scrElement.src ='';
scrElement.src ='http:// oneBigHairyURL';
setTimeout(loadItUp,10000);
};
loadItUp();
< / script>

我得到警报,div清除,但没有动态生成的HTML被重新加载。 / p>

任何想法我做错了什么?

解决方案

在脚本中添加一个新的脚本标签到< head>(重新)加载?如下所示:

 < script> 
函数load_js()
{
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.src ='source_file.js';
head.appendChild(script);
}
load_js();
< / script>

最重要的是插入一个新的脚本标签 - 您可以删除旧的脚本标签。如果您有缓存问题,您可能需要为查询字符串添加时间戳。


I have a page that is loading a script from a third party (news feed). The src url for the script is assigned dynamically on load up (per third party code).

<div id="div1287">
    <!-- dynamically-generated elements will go here. -->
</div>

<script id="script0348710783" type="javascript/text">
</script>

<script type="javascript/text">
    document.getElementById('script0348710783').src='http://oneBigHairyURL';
</script>

The script loaded from http://oneBigHairyURL then creates and loads elements with the various stuff from the news feed, with pretty formatting, etc. into div1287 (the Id "div1287" is passed in http://oneBigHairyURL so the script knows where to load the content).

The only problem is, it only loads it once. I'd like it to reload (and thus display new content) every n seconds.

So, I thought I'd try this:

<div id="div1287">
    <!-- dynamically-generated elements will go here. -->
</div>

<script id="script0348710783" type="javascript/text">
</script>

<script type="javascript/text">
    loadItUp=function() {
        alert('loading...');
        var divElement = document.getElementById('div1287');
        var scrElement = document.getElementById('script0348710783');

        divElement.innerHTML='';
        scrElement.innerHTML='';
        scrElement.src='';
        scrElement.src='http://oneBigHairyURL';
        setTimeout(loadItUp, 10000);
    };
    loadItUp();
</script>

I get the alert, the div clears, but no dynamically-generated HTML is reloaded to it.

Any idea what I'm doing wrong?

解决方案

How about adding a new script tag to <head> with the script to (re)load? Something like below:

<script>
   function load_js()
   {
      var head= document.getElementsByTagName('head')[0];
      var script= document.createElement('script');
      script.src= 'source_file.js';
      head.appendChild(script);
   }
   load_js();
</script>

The main point is inserting a new script tag -- you can remove the old one without consequence. You may need to add a timestamp to the query string if you have caching issues.

这篇关于如何强制脚本重新加载并重新执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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