Solved

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

Posted on 2002-04-08
15
626 Views
Last Modified: 2008-02-01
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
Comment
Question by:DanielBlais
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
  • 2
  • +1
15 Comments
 
LVL 23

Accepted Solution

by:
b1xml2 earned 100 total points
ID: 6925550
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 6925556
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
 
LVL 33

Expert Comment

by:knightEknight
ID: 6925563
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 23

Expert Comment

by:b1xml2
ID: 6925586
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
 
LVL 2

Author Comment

by:DanielBlais
ID: 6925702
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 6927099
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 6927100
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
 
LVL 2

Author Comment

by:DanielBlais
ID: 6927696
I have find a sinple way.

I have add in header of my file.

<%
response.expires = -1
%>


0
 
LVL 2

Author Comment

by:DanielBlais
ID: 6927699
Is there a way to give point to 2 persons?
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6927704
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 6927766
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 6927768
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 6928778
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
 
LVL 5

Expert Comment

by:Netminder
ID: 6928795
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
 
LVL 23

Expert Comment

by:b1xml2
ID: 6928811
Netminder, the question is worth 200 points, pls rectify...
my share is 100 points and not 10 points
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question