Ivo said the following on 6/25/2006 5:07 PM:[color=blue]
> <max@kipness.com> schreef[color=green]
>> Based on various posts, I've come up with the following code to load
>> data into a js array dynamically without a screen refresh. It works,
>> but you have to push the button twice before it shows the new data that
>> you have changed in the js file. It's almost like something is being
>> cached.
>>
>> <html>[/color]
>
> No doctype?[/color]
<sigh> It never ends.
[color=blue][color=green]
>> <head>[/color]
>
> No title?
>[color=green]
>> <script id="mydata" src="test.js" type="text/javascript"></script>
>> <script>[/color]
>
> No type attribute?[/color]
Irrelevant and not needed except by the validator. And in the absence of
something to the contrary, it will default to Javascript.
[color=blue][color=green]
>> var mysplitdata;
>> function refresh_data () {
>> document.getElementById('mydata').src='test.js?x=' +
>> Math.random();[/color][/color]
That line will only work in IE. If you want to dynamically load .js
files on the fly in a cross browser manner then you dynamically create
the script element:
function loadJSFile(fileURL){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.src = fileURL;
document.getElementsByTagName('head')[0].appendChild(newScript);
}
and then call it like this:
loadJSFile('URLToJSFile');
It needs some feature detection added, and it can have some other
fallback paths but that is the basic script needed in modern browsers.
[color=blue][color=green]
>> mysplitdata = myData.split(",");[/color]
>
> What is the variable myData?[/color]
It is an array in the test.js file that was posted.
[color=blue]
> You don't mean to refer to the element with id "mydata", do you?[/color]
No, its a reference to an array. And even if it were the element with id
of mydata a script element doesn't have a split().
[color=blue]
> Remember Javascript is very sensitive to case: D is not d.[/color]
It's a good thing or the above code would error out.
[color=blue]
> Also, the split() method is a method of strings. You can't split an element
> with it, only its contained textnode.[/color]
Again, it is a reference to an array that is in the external file. The
code for it is in the original post.
[color=blue]
> Reading on in your post, I find that myData is the variable to download.[/color]
Then why all the ranting/rambling about myData versus mydata?
[color=blue]
> But a new file takes some time, however short, to download.[/color]
Precisely the problem. A timing issue.
[color=blue]
> You can't expect its content to be available on the very next line in your script.[/color]
You can expect it, but you will be disappointed.
[color=blue]
> IE may have some events that you could bind a handler to, onreadystatechange,
> onload for script files, but none of these are cross-browser compatible.[/color]
And none of them are needed.
[color=blue]
> It may an idea to put the commands to run after the file has loaded, in the
> file itself. Or at least the call to a function that processes the data.[/color]
That is the only reliable way to solve the problem in a cross browser
manner.
..js file:
//data
someFunctionInThePageItselfThatProcessesThisData()
--
Randy
comp.lang.javascript FAQ -
http://jibbering.com/faq & newsgroup weekly
Temporarily at:
http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/