LemonInflux Posted March 16, 2008 Share Posted March 16, 2008 Hi. Recently, I've started looking into AJAX for my projects. I've been following the W3S tutorial, and it's fine. But, I have a question. I have one AJAX function, like this: <script type="text/javascript"> function ajaxFunction() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.myForm.time.value=xmlHttp.responseText; } } xmlHttp.open("GET","time.php",true); xmlHttp.send(null); } </script> What if I want another AJAX function, like this: <script type="text/javascript"> function ajaxFunction() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.myForm.time.value=xmlHttp.responseText; } } xmlHttp.open("GET","getComments.php",true); xmlHttp.send(null); } </script> Do I really need to include all of the first part in both, or is there a way of trimming down these two so they're a bit more manageable? Any help appreciated Thanks in advance, Tom Quote Link to comment Share on other sites More sharing options...
priti Posted March 18, 2008 Share Posted March 18, 2008 yes you can,define the general function only once and you can have two different vars. i.e xmlhttp=ajaxFunction(); xmlrequest=ajaxFunction(); because ajaxFunction() is responsible for creating the xmlhttprequest object then how you deal will depends on you. Regards Quote Link to comment Share on other sites More sharing options...
XoSilenceoX Posted March 18, 2008 Share Posted March 18, 2008 You could set variables within the function example: <script type="text/javascript"> function ajaxFunction(AJAXpage) { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.myForm.time.value=xmlHttp.responseText; } } xmlHttp.open("GET","AJAXpage",true); xmlHttp.send(null); } </script> then all you would have to do is call it with ajaxFunction(link/to/page.here); you can use that over and over all over the page and not need to create multiple scripts. 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.