Hello, i have small piece of code that works perfectly in Chrome, but does not work in Firefox. function loadXMLDoc() { var list=''; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { console.log(xmlhttp.readyState); if (xmlhttp.readyState==4 && xmlhttp.status==200) { list=xmlhttp.responseText; } } xmlhttp.open("GET","http://www.domain.net/list.txt",false); xmlhttp.setRequestHeader("Content-Type", "text/plain"); xmlhttp.send(); console.log(list); return list; } Code (markup): in firefox 'list.txt' is downloaded perfectly but 'list' is empty variable.
Okay so the false part of this line: xmlhttp.open("GET","http://www.domain.net/list.txt",false) is saying that you don't want to load the data asynchronously. This means that execution will halt until the data has been loaded... I am guessing that chrome is executing the 'onreadystatechange' function before the next lines of code where firefox isn't. Try: function loadXMLDoc() { var list=''; if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","http://www.domain.net/list.txt",false); xmlhttp.setRequestHeader("Content-Type", "text/plain"); xmlhttp.send(); list=xmlhttp.responseText; console.log(list); return list; } Code (markup):
If you want to do ajax, I strongly recommend you use jquery. You just need to make a function call and it does everything for you.