calling function from onload doesnt work

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

calling function from onload doesnt work

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I'm not very experienced with AJAX, but don't you have to wait for the response before attempting to use it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What does "loadGoogleMap" have to do with "request_markers"?
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Again, you have to await the response before there is any information to process. It isn't instant.
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post 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
sarris
Forum Contributor
Posts: 137
Joined: Mon Dec 04, 2006 2:44 pm

Post by sarris »

stupid me...

Code: Select all

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

thanks.
Post Reply