Hi Erwan,
The problem is that i is not defined when the function(xml, url)
{ Profil[i].loadXML(xml, url);} is called by Timeline.
(Different scope.)
In most cases, Javascript will maintain scope. Eg
function foo(a, b) {
function foo2 () {
// has access to a & b
}
}
But in this case, perhaps because i is only local to the block, not
the surrounding function, it isn't working for you.
Remember that the function will be called by the browser in a
different Javascript thread when the response comes back from the
server with your data file.
I suggest:
1) Trying making i a variable for the surrounding function:
var i;
for (i = 0; i < nb_file_xml; i++) {
var fichier = "Profil" + (i+1) + ".xml";
tl.loadXML(fichier, function(xml, url) { Profil[i].loadXML(xml,
url); });
}
2) The url is passed to the function. The function can parse it to see
which Profil it should be loading.
3) A fancier solution is to create a function, on the fly, that uses a
constant.
Eg
for (var i = 0; i < nb_file_xml; i++) {
var fichier = "Profil" + (i+1) + ".xml";
var myfunc = new Function(xml, url, "Profil[" + i + "].loadXML
(xml, url);");
// dynamic programming! Should only be used if absolutely
necessary
tl.loadXML(fichier, myfunc);
}
Google for Javascript scope for more.
Good luck,
Larry
On Nov 11, 11:46 am, Erwan <[EMAIL PROTECTED]> wrote:
> Hello everyone :)
> please excuse my bad english. i hope some help from you please :)
>
> when i use :
>
> for (var i = 0; i < nb_file_xml; i++) {
> var fichier = "Profil" + (i+1) + ".xml";
> tl.loadXML(fichier, function(xml, url) {
> Profil[i].loadXML(xml,
> url); });
> }
>
> i have an alert box with : Caught exception : Type Error : Profil[i]
> is undefined
>
> when i use :
> tl.loadXML("Profil1.xml", function(xml, url) { Profil[0].loadXML(xml,
> url); });
> tl.loadXML("Profil2.xml", function(xml, url) { Profil[1].loadXML(xml,
> url); });
>
> it works fine.
>
> here is the complete code :
>
> <html>
> <head>
> <title>SIMILE | Timeline | Profils</title>
> <script src="http://simile.mit.edu/timeline/api/timeline-api.js"
> type="text/javascript"></script>
> <script src="fonctions.js" type="text/javascript"></script>
> <script>
> var tl;
> function onLoad() {
> var url = window.location.href;
> var Profil = new Array();
> var bandInfos = new Array();
> var nb_file_xml = 2;
> var taille = 100/nb_file_xml;
> var d = "1758"
>
> var theme = Timeline.ClassicTheme.create();
> theme.event.bubble.width = 320;
> theme.event.bubble.height = 220;
> theme.event.track.height = 1.5;
> theme.event.track.gap = 0.1;
> theme.ether.backgroundColors = [
> "#D1CECA",
> "#E7DFD6",
> "#E8E8F4",
> "#D0D0E8"
> ];
>
> for (var i = 0; i < nb_file_xml; i++) {
>
> Profil[i] = new Timeline.DefaultEventSource();
> bandInfos[i] = Timeline.createBandInfo({
> width: taille + "%",
> intervalUnit: Timeline.DateTime.YEAR,
> intervalPixels: 100,
> eventSource: Profil[i],
> date: d,
> timeZone: 0,
> theme: theme
> });
> }
>
> for (var i = 0; i < nb_file_xml-1; i++) {
> bandInfos[i].syncWith = i+1;
> }
>
> tl = Timeline.create(document.getElementById("tl"), bandInfos,
> Timeline.HORIZONTAL);
>
> //tl.loadXML("Profil1.xml", function(xml, url)
> { Profil[0].loadXML(xml, url); });
> // tl.loadXML("Profil2.xml", function(xml, url)
> { Profil[1].loadXML(xml, url); });
>
> for (var i = 0; i < nb_file_xml; i++) {
> var fichier = "Profil" + (i+1) + ".xml";
> tl.loadXML(fichier, function(xml, url) {
> Profil[i].loadXML(xml,
> url); });
> }
> }
>
> var resizeTimerID = null;
> function onResize() {
> if (resizeTimerID == null) {
> resizeTimerID = window.setTimeout(function() {
> resizeTimerID = null;
> tl.layout();
> }, 500);
> }
> }
> </script>
> </head>
> <body onload="onLoad();" onresize="onResize();">
>
> <div id="body">
> <h1>Profils Timeline</h1>
> <div id="tl" style="height: 80%">
> </div>
>
> </div>
> </body>
> </html>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"SIMILE Widgets" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/simile-widgets?hl=en
-~----------~----~----~----~------~----~------~--~---