Page 1 of 1

calling function from onload doesnt work

Posted: Sat Jul 07, 2007 11:37 am
by sarris
Hi there,
I am encoutering a weird problem,
I call this function on the body onload event

Code: Select all

if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
	map.setCenter(new GLatLng(37.97242, 23.728), 6);
	
        map.markers=[];
	request_markers();
				
		GEvent.addListener(map, 'zoomend', function() {
			drawMarkers(filteredMarkers(map.markers));
		});
		
		test();

	}
}
and test() function is just

Code: Select all

function test(){
alert(map.markers.length);
}
map is declared globaly.
My problem is that the function test() shows 0 when run from within the onload function, but when i assign it to a click of a button it shows the proper ammount of markers produced by the request_markers() function...

Why is this happening?
Thanks

Posted: Sat Jul 07, 2007 11:42 am
by superdezign
What? Where's the function you are calling on 'load'? What is the definition of request_markers? And where do you set your 'load' event handler?

You haven't given nearly enough code to solve this. Usually the only time something doesn't get called on the 'load' event is because people assign the event incorrectly.

Posted: Sat Jul 07, 2007 1:26 pm
by sarris
this is how the onload is called

Code: Select all

<body onload="loadGoogleMap()">
here is the request_markers() function

Code: Select all

function request_markers(){


	var url = "http://www.myurl.com/markers.xml";
	var http_request = getHTTPObject();
        http_request.open("GET", url, true);
        http_request.onreadystatechange = function() {
               if(http_request.readyState == 4) {
                    if(http_request.status == 200){

                           GetMarkers(http_request);

                   }
            }
	}
       http_request.send(null);
}
thanks again

Posted: Sat Jul 07, 2007 2:00 pm
by superdezign
I'm not very experienced with AJAX, but don't you have to wait for the response before attempting to use it?

Posted: Sat Jul 07, 2007 4:01 pm
by feyd
What does "loadGoogleMap" have to do with "request_markers"?

Posted: Sat Jul 07, 2007 8:13 pm
by sarris
What does "loadGoogleMap" have to do with "request_markers"?
request_marker() as hard it is to see, is called within loadGoogleMap and this was answering to "superdesign" when he asked
Where's the function you are calling on 'load'? What is the definition of request_markers?
the obvious thing is that when i call the test() function after the onload function is finished everything works fine, but when i call it from withing the loadGoogleMap() function it doesnt.

why is that?

Posted: Sat Jul 07, 2007 8:25 pm
by superdezign
Again, you have to await the response before there is any information to process. It isn't instant.

Posted: Tue Jul 10, 2007 8:42 am
by sarris
aaah, ok i see.
I entered a timeout and everything works ok.
However is the timeout the proper way to do that? Is there another way to know exactly when i get the response?

thanks anyway

Posted: Tue Jul 10, 2007 8:56 am
by sarris
stupid me...

Code: Select all

if(http_request.status == 200)
does exactly that.

thanks.