Jay Braun
9/4/2015 10:48:00 PM
> I propose to insert a script element dynamically. I think that
> is more elegant anyway.
Hello. Prior to reading this response, I tried something like that, i.e., creating a script element dynamically, and calling the desired function, which comprises the created script. That works, but there's one remaining (big) problem: Even though the second script is re-generated periodically, the logic in the first version is executed. It's as if the second script has already been loaded into memory, and I cannot get rid of it, even with extreme measures.
Here is my most recent attempt, with extreme measures to "wipe out" the script element before creating a new one. The function repaint() comprises the called script, a new version of which is generated periodically by a conventional C program running on the server machine.
setInterval(
function() {
var fileref;
if (fileref===undefined) {
console.log("creating script");
fileref=document.createElementNS (xmlns, "script");
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", "var.js");
console.log("calling repaint");
repaint(xmlns, svgElem);
fileref.remove();
fileref=undefined;
}
}, 3000 );
Note that the messages logged to the browser console are being output every 3 seconds. Each call to repaint executes the logic of the first version of the script, even though it has been overwritten.
I get the same results whether or not the outer html file defines var.js as a script. The scrpt containing the above code, fixed.js, is defined in the html file as a script. Here is my index.html with both .js files defined:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>Ping!</title>
<style>
#svgContainer {
width: 2401px;
height: 2401px;
}
</style>
<script type='text/javascript' src='fixed.js'></script>
<script type='text/javascript' src='var.js'></script>
</head>
<body onload="resagui()">
<div id="svgContainer"></div>
</body>
Incidentally, my application *does* execute new versions of var.js every 5 seconds if, instead of the JavaScript logic above, I put the following in the <title> section:
<meta http-equiv="refresh" content="5">
But that also re-execues fixed.js, which retrieves a large .png image from the server. It's very time-consuming.
Thank you for any ideas you can offer.
Jay