开发者

Reload file.js every minute

开发者 https://www.devze.com 2023-02-03 16:53 出处:网络
I read (somewhere else on this site) you can\'t reload (or inject javascript) onto a page that is already rendered.

I read (somewhere else on this site) you can't reload (or inject javascript) onto a page that is already rendered. Is there any other way of doing this. For instance an iFrame?

I have a recent comment widget.js and I need to constantly get it to reload without reloading the whole page.

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. I want it to update itself, a way to do this is to just reload the js file on开发者_如何学JAVA the page, correct?


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.

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.


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

Once you get the data from the 3rd party, you could probably use some combination of regex and JSON parser to extract the comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消