Link to home
Start Free TrialLog in
Avatar of DanielBlais
DanielBlaisFlag for Canada

asked on

reload <script language="javascript" src=...>

I want to reload an include javascript at each x seconds.

It works well in NS6, but do nothing in IE.

Any idea?

Here my code :

<div id="test"></div>

<script language="javascript">
function showInfo()
{
  var info;
 
  info = "<scr" + "ipt language='jav" + "ascript' src='rfm-info.htm'></scr" + "ipt>";
  document.getElementById("test").innerHTML = info;
 
  window.setTimeout("showInfo();",1000);
}
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of knightEknight
here is how to do it in IE:

<SCRIPT language='JavaScript'>

 function include(url)  // must be a fully qualified URL, I don't know why
 {
   var xml = new ActiveXObject("Microsoft.XMLHTTP");
   xml.Open( "GET", url, false );
   xml.Send()
   document.writeln(xml.responseText);
 }

</script>
so the following works with both MSIE5+ and NS6+

<html>
<head>
<title>Loading Scripts</title>
<script language="javascript">
window.onload = init;
function init() {
    eData = document.getElementById("data");
     if (! document.all) {
          eData.innerHTML = "<scr" + "ipt language='jav" + "ascript' src='scripts/data.js'></scr" + "ipt>";
     
     } else {
          eScript = document.createElement("script");
          eScript.setAttribute("language","javascript");
          eData.appendChild(eScript);
          eScript.setAttribute("src","scripts/data.js");
     }
     window.setInterval("loadData();",1000);    
}

function loadData() {
     if (! document.all) {
          eData.innerHTML = "<scr" + "ipt language='jav" + "ascript' src='scripts/data1.js'></scr" + "ipt>";
     } else {
          eScript.setAttribute("src","scripts/data1.js");
     }
}
</script>
</head>
<body>
<div id="data"></div>
</body>
</html>

where data.js and data1.js is just to show that you can swap files.
Avatar of DanielBlais

ASKER

sorry, none of this example works.

b1xml2, your example works the first time, but if I change the source file, it doesn't refresh (seem like an cache problem).

knightEknight, the whole page disappear and only the source of the included file appear.

You can see my page at :
http://213.41.71.8/rfm/rfminfo-main.htm
and
http://213.41.71.8/rfm/rfminfo-main2.htm

DanielBlais,

whether you use XMLHTTP or the SCRIPT element to reload data, it is governed by the browser settings and if the browser settings is not set to check for newer versions on every visit to the page, you may or may not get the data from the server instead of from the cache.

That is where using IFRAMES provides the ability to reload the IFRAME with the option to get the data from the server.

window.frames["myframe"].window.location.reload(true);

unless of course, you vary the source path e.g.
first pass data.js
second pass data.js?timestamp=200204031000
third pass data.js?timestamp=2002040310000

thus try this for size
======================

<html>
<head>
<title>Loading Scripts</title>
<script language="javascript">
window.onload = init;
function init() {
    eData = document.getElementById("data");
     if (! document.all) {
          eData.innerHTML = "<scr" + "ipt language='jav" + "ascript' src='scripts/data.js'></scr" + "ipt>";
     
     } else {
          eScript = document.createElement("script");
          eScript.setAttribute("language","javascript");
          eData.appendChild(eScript);
          eScript.setAttribute("src","scripts/data.js?timestamp=2000");
     }
     window.setInterval("loadData();",2000);    
}

function loadData() {
     var szURL = "scripts/data1.js?timestamp=" + getTimeStamp();
     //alert(szURL);
     if (! document.all) {
          eData.innerHTML = "<scr" + "ipt language='jav" + "ascript' src='" + szURL + "'></scr" + "ipt>";
     } else {
          eScript.setAttribute("src",szURL);
     }
}

function getTimeStamp() {
     var datNow = new Date();
     var szDate = datNow.getFullYear();
     szDate += (datNow.getMonth() < 9 ? "0" : "") + (datNow.getMonth() + 1);
     szDate += (datNow.getDate() < 10 ? "0" : "") + datNow.getDate();
     szDate += (datNow.getHours() < 10 ? "0" : "") + datNow.getHours();
     szDate += (datNow.getMinutes() < 10 ? "0" : "") + datNow.getMinutes();
     szDate += (datNow.getSeconds() < 10 ? "0" : "") + datNow.getSeconds();
     switch (String(datNow.getMilliseconds()).length) {
     case 2:
     szDate += "0";
    break;      
     case 1:
      szDate += "00";
     }
     szDate += datNow.getMilliseconds();
     return szDate;
}
</script>
</head>
<body>
<div id="data"></div>
</body>
</html>
just a slight amendment to the init function,

function init() {
    eData = document.getElementById("data");
     if (! document.all) {
          eData.innerHTML = "<scr" + "ipt language='jav" + "ascript' src='scripts/data.js'></scr" + "ipt>";
     
     } else {
          eScript = document.createElement("script");
          eScript.setAttribute("language","javascript");
          eData.appendChild(eScript);
          eScript.setAttribute("src","scripts/data.js");
     }
     window.setInterval("loadData();",2000);    
}
I have find a sinple way.

I have add in header of my file.

<%
response.expires = -1
%>


Is there a way to give point to 2 persons?
yeah request a split in the Community Support. what they will do is the following.

1. They will reduce the points here and refund the points to you.

2. You award one of the experts here with the split points.

3. You post another question with the refunded points with the title e.g. For expertABCD.
In the post, say something like this:

"For assistance in  https://www.experts-exchange.com/jsp/qShow.jsp?qid=20286072"
hmmm, why the grade C when you did not tell us you were using IIS,

FYI.
'This expires the document immediately.
Response.Expires = -1
'This prevents proxy servers from caching the content
Response.CacheControl = "no-cache"
'This tells MSIE to not keep a cache copy
Response.AddHeader "Pragma","no-cache"

The dynamic URL will result in the javascript being called because it is not cached, having been called only once in a lifetime.
The dynamic URL will result in the javascript being called from the server because it is not cached, having been called only once in a lifetime.
whichever moderator that modified this question, kindly note the following.

1. This QID is worth 200 points, why reduce it to 10 points for me? Where is the justice when all I wanted was an equitable outcome for knightEKnight as well in
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?qid=20286462

Please rectify immediately.
Avatar of Netminder
Netminder

Grade changed, and split recorded per request of Asker.

knightEknight: points for you at https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20286592

Netminder
CS Moderator
Netminder, the question is worth 200 points, pls rectify...
my share is 100 points and not 10 points