简体   繁体   English

每分钟重新加载file.js

[英]Reload file.js every minute

I read (somewhere else on this site) you can't reload (or inject javascript) onto a page that is already rendered. 我读到了(在本网站的其他地方)您不能重新加载(或注入javascript)到已经渲染的页面上。 Is there any other way of doing this. 还有其他方法可以做到这一点。 For instance an iFrame? 例如iFrame?

I have a recent comment widget.js and I need to constantly get it to reload without reloading the whole page. 我最近有一个注释widget.js,我需要不断地重新加载它而不重新加载整个页面。

Any ideas? 有任何想法吗?

edit: The site has recent comments on it and they are displayed via a recentcomment.js Once the page is loaded it doesn't update itself unless you reload the page. 编辑:该站点上有最近的注释,并通过lastestcomment.js显示。页面加载后,除非您重新加载页面,否则它不会自动更新。 I want it to update itself, a way to do this is to just reload the js file on the page, correct? 我希望它可以自我更新,一种方法是重新加载页面上的js文件,对吗?

Why do you need to do this? 为什么需要这样做? It seems to me that there's probably a more appropriate solution to your problem. 在我看来,可能有一个更合适的解决方案来解决您的问题。

But to answer it: 但是要回答:

var elm = document.createElement("script");
elm.src = "Widget.js";
document.getElementsByTagName("head")[0].appendChild(elm);

Hope I didn't write any mistakes... 希望我没有写任何错误...

Rather than reloading the file, you can have all the implementation of the file contained in a function and then call the function every minute using the setTimeout() function. 无需重新加载文件,您可以使函数中包含文件的所有实现,然后使用setTimeout()函数每分钟调用一次该函数。

Alternatively, if you want to reload it because the content of the file might have changed, it would probably be better to move that part of the code out to some external file and then use a function (running every minute with setTimeout() ) to load the new content you need. 另外,如果由于文件内容可能已更改而要重新加载它,则最好将代码的那部分移到某个外部文件中,然后使用一个函数(每隔分钟运行一次setTimeout() )来进行操作,加载所需的新内容。

You can make cross-domain AJAX requests using certain methods, so you could make an AJAX request for the script file and parse it yourself. 您可以使用某些方法发出跨域AJAX请求,因此可以对脚本文件发出AJAX请求并自己解析。 Parsing it yourself probably isn't an optimal solution, but it looks like you're dealing with a brain-dead service provider anyways. 自己解析它可能不是一个最佳解决方案,但无论如何,您似乎正在与一个脑瘫的服务提供商打交道。

Look at this guy's jQuery mod for an example: http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/ 以这个人的jQuery mod为例: http : //james.padolsey.com/javascript/cross-domain-requests-with-jquery/

Once you get the data from the 3rd party, you could probably use some combination of regex and JSON parser to extract the comments. 一旦从第三方获得数据,就可以使用正则表达式和JSON解析器的某种组合来提取注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM