开发者

For every AJAX call I make, I would like to reload a new java-script

Basically, everytime anybody does anything on this website, I need to retrieve a new javascript from the server (it is a complex math thing, and I am not concerned with speed).

I make the AJAX call, and stick it in the browser in a tag, like this:

getandplaceajax('id=showtotals','main');   // The first is the URL parameter, and the second is the ID of the <div> tag.

While I am doing this, I would like to re-write the java-script file, on the server, and then reload it. It is like this, in the tag.

<script type="text/javascript" src="randomfilename.js"></script>

After this I refresh my object thusly, by retreiving new XML data:

    object1.loadXML("http://mywebsite/mydata.xml",
       function(xml, url) {eventSource.loadXML(xml,url); });

How do I tell the browser to re-load the java-script file (force a re-load, on demand)?

I tried to interactively load the java-script into the portion of the page, but this is an iffy situation given that AJA开发者_如何学PythonX is asynchronous and unpredictable in the event chain.

And I am not doing page loads, so referencing the java-script with a unique number parameter (to prevent caching) isn't an option.

An extra $50 in the church offering plate next Sunday, in your name, for a solution.

Thanks in advance

Jeff


There is no need to make an Ajax call if you just need to load a JavaScript file. You can just append a new script tag to the page.

var scr = document.createElement("script");
scr.type="text/javascript";
scr.src="myFile.js";
document.body.appendChild(scr)

If you are calling the same file each time you need to force it to fetch the new file by adding a querystring value

scr.src="myFile.js?ts=" + new Date().getTime();

On the server you can request a php, servlet, .NET page, etc and have it return the JavaScript code. It does not have to be a js file. Just set the content type to be JavaScript and your browser will not care.


If you are not using any library, you can use what Facebook is doing

http://developers.facebook.com/docs/reference/javascript/

var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
  '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);

to load in a new script this way. You can also change the src of the script tag, to something like 'myfile.js?timestamp=' + (new Date()).getTime(), or you can return javascript with <script> tag around it and put it into a div or return javascript and eval them, similar to how RJS is handled.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜