Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 683
  • Last Modified:

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);
}
0
DanielBlais
Asked:
DanielBlais
  • 9
  • 3
  • 2
  • +1
1 Solution
 
b1xml2Commented:
under MSIE,

<html>
<head>
<title>Loading Scripts</title>
<script language="javascript">
window.onload = init;
function init() {
     eScript = document.createElement("script");
     eScript.setAttribute("language","javascript");
     document.getElementById("data").appendChild(eScript);
     eScript.setAttribute("src","scripts/data.js");
     window.setInterval("loadData();",200);    
}

function loadData() {
     eScript.setAttribute("src","scripts/data1.js");
}
</script>
</head>
<body>
<div id="data"></div>
</body>
</html>


scripts/data.js
===============
alert("data.js");

scripts/data1.js
================
alert("data1.js");

Notes
=====
1. Tested with MSIE 5+
2. Wont work with NS 6.2.x

I provided two scripts so that you can see the second script loaded.
0
 
knightEknightCommented:
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>
0
 
knightEknightCommented:
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
b1xml2Commented:
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.
0
 
DanielBlaisAuthor Commented:
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

0
 
b1xml2Commented:
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>
0
 
b1xml2Commented:
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);    
}
0
 
DanielBlaisAuthor Commented:
I have find a sinple way.

I have add in header of my file.

<%
response.expires = -1
%>


0
 
DanielBlaisAuthor Commented:
Is there a way to give point to 2 persons?
0
 
b1xml2Commented:
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  http://www.experts-exchange.com/jsp/qShow.jsp?qid=20286072"
0
 
b1xml2Commented:
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.
0
 
b1xml2Commented:
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.
0
 
b1xml2Commented:
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
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?qid=20286462

Please rectify immediately.
0
 
NetminderCommented:
Grade changed, and split recorded per request of Asker.

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

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now