如何强制脚本重新加载并重新执行? [英] How to force a script reload and re-execute?
问题描述
我有一个从第三方加载脚本的页面(新闻提要)。
脚本的src
< div id =div1287>
<! - 动态生成的元素将在此处显示。 - >
< / div>
< script id =script0348710783type =javascript / text>
< / script>
< script type =javascript / text>
document.getElementById('script0348710783').src ='http:// oneBigHairyURL';
< / script>
从 唯一的问题是,它只加载一次。我希望它每n秒重新加载一次(因此显示新内容)。 所以,我想我会试试这个: 我得到警报,div清除,但没有动态生成的HTML被重新加载。 / p> 任何想法我做错了什么? 在脚本中添加一个新的脚本标签到< head>(重新)加载?如下所示: 最重要的是插入一个新的脚本标签 - 您可以删除旧的脚本标签。如果您有缓存问题,您可能需要为查询字符串添加时间戳。 I have a page that is loading a script from a third party (news feed). The The script loaded from 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: 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: 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屋! http:// oneBigHairyURL
然后用新闻提要中的各种东西创建和加载元素,格式很漂亮,等等< http:// oneBigHairyURL
,这样脚本知道在哪里加载内容)。
< 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>
< 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>
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>
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).<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>
<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>