Solved

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

Posted on 2002-04-08
15
608 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
  • 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now