Link to home
Start Free TrialLog in
Avatar of walkman69
walkman69

asked on

passing an array of functions as an argument

Hi guys.

I have made something like this:


function init()
{
	var gnu = [init_function_01(), init_function_02()];
	ajax_get_page("some_page_01.php", "", "content_center", gnu);
}

function init_function_01() {
	console.log("gnu1");
}

function init_function_02() {
	console.log("gnu2");
}

function ajax_get_page(page, arg, elem, init_functions) {
	
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else {
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {	
			document.getElementById(elem).innerHTML=xmlhttp.responseText;
			
			for(x = 0; x < init_function.length; x++) {
				init_functions[x];
			}
		}
	}
	
	xmlhttp.open("GET",page+arg,true);
	xmlhttp.send();	
}

Open in new window


The init-function is called on load.

So I try to get a page and place the output inside an HTML-element called content_center,
and if the page has been correctly loaded, a couple of functions are supposed to be called.
However.. this doesn't work since the functions are called on start. And then they aren't called at all.. I think I lack some knowledge here..

Anyone got a good solve for this?


Avatar of leakim971
leakim971
Flag of Guadeloupe image

We put the array of function init_functions as global var and the elem string too :

      var init_functions = [];
      var elem = "";
      

We do a synchrone call : xmlhttp.open("GET", page + arg , false);



<script language="javascript">

	var init_functions = [];
	var elem = "";
	
	function init()
	{
		init_functions = [init_function_01(), init_function_02()];
		elem = "";
		ajax_get_page("some_page_01.php");
	}

	function init_function_01() {
		console.log("gnu1");
	}

	function init_function_02() {
		console.log("gnu2");
	}

	function ajax_get_page(page, arg) {
	
		if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
		
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState==4 && xmlhttp.status==200) {	
				document.getElementById(elem).innerHTML = xmlhttp.responseText;		
				for(x = 0; x < init_function.length; x++) {
					init_functions[x];
				}
			}
		}
		
		xmlhttp.open("GET", page + arg , false);
		xmlhttp.send();	
	}
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of walkman69
walkman69

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial