EdgeWalker Posted May 2, 2008 Share Posted May 2, 2008 I want to be able to pass a variable to a callback function for my AJAX base, but the only way I could think of to keep the variable in scope was to put the callback function INSIDE the main one. Is that ok? Better solutions? function get_and_replace(url,div_id) { //Replaces the innerHTML of the div with the response from the the URL createXMLHttpRequest(); xmlHttp.open("GET",url); xmlHttp.onreadystatechange=callback; xmlHttp.send(null); function callback() { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { //HERE IS WHERE IS USE THE VARIABLE div_id div_element=document.getElementById(div_id); div_element.innerHTML=xmlHttp.responseText; } } } } (I thought this was more of a general JS question than AJAX) Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 3, 2008 Share Posted May 3, 2008 maybe you could use a global //declare global outside function var global_div_id function get_and_replace(url,div_id) { global_div_id=div_id //Replaces the innerHTML of the div with the response from the the URL createXMLHttpRequest(); xmlHttp.open("GET",url); xmlHttp.onreadystatechange=callback; xmlHttp.send(null); } function callback() { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { //HERE IS WHERE IS USE THE VARIABLE div_id div_element=document.getElementById(global_div_id); div_element.innerHTML=xmlHttp.responseText; } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.