javascript - How to always refresh file loaded by ajax, avoiding cached file -
first load external javascript file following code (it appends ext_chat.js
script source current dom):
var protocol = ('https:' == document.location.protocol ? 'https://' : 'http://'); (function(d, t, p) { var g = d.createelement(t), s = d.getelementbyid('chat_script'); g.src = p + 'mydomain.lc/js/ext_chat.js'; s.parentnode.insertbefore(g, s.nextsibling); }(document, 'script', protocol));
inside ext_chat.js
. here i'm including css file:
function r(f) { /in/.test(document.readystate) ? settimeout('r(' + f + ')', 9) : f() } r(function () { includecssfile(getbaseurl() + '/css/ext_chat.css')); // getbaseurl() gives correct url }); function includecssfile(href) { var head_node = document.getelementsbytagname('head')[0]; var link_tag = document.createelement('link'); link_tag.setattribute('rel', 'stylesheet'); link_tag.setattribute('type', 'text/css'); link_tag.setattribute('href', href); link_tag.setattribute('media', 'screen'); head_node.appendchild(link_tag); }
on first load, css file included fine. after first time, when page loaded changes ext_chat.css
file not reflected, , cached file being used instead. how can force css file reload every time page loads, instead of referring cached one?
first things first, don't use eval version of settimeout
. use .bind
instead.
/in/.test(document.readystate) ? settimeout(r.bind(null, f), 9) : f()
now, avoid file caching should attach random query string end of uri. you'll see ?v=123456789
or effect. can use timestamp in case.
r(function () { includecssfile(getbaseurl() + '/css/ext_chat.css?v=' + date.now())); // getbaseurl() gives correct url });
reading material:
Comments
Post a Comment