How would I cause JS to goto a URL and return the page/text that it gets? What I mean is, I need JS to go to a txt file on a server and use the file’s contents as a variable.
so depending on the browser ,you need to make a new instant of xmlhttp. In other words if you want to make sure it works for all browsers,then you have some extra code to write .
heres a little demo to get you started :[code=php] function doAjax(url , getOrPost , callBackFn , params ,contentIsXml ){ var xmlHttp = null; if(typeof XMLHttpRequest!='undefined') { xmlHttp = new XMLHttpRequest(); //firefox based }else{ //ie based try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { try { xmlHttp = new XMLHttpRequest(); } catch (e) { alert('sorry ,this browser doesnt support xmlhttp the way youve done it,do some more research'); return ; } } } } if(! ( (contentIsXml!=undefined)?contentIsXml:true){ xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); } if(url!=undefined){url+='?now='+(new Date().getTime() ); }else{return;} xmlHttp.open(getOrPost, url, true); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState==4 && xmlHttp.status==200) { //all done ! callBackFn(xmlHttp); }else{ //display a 'loading...' message if you want... } } if(params!=undefined){ xmlHttp.send(params); }else{ xmlHttp.send(null); } }
} [/code]
and heres the implementation [code=php] doAjax('path/to/txtfile.txt','GET', readDone);
function readDone(req){ alert( 'the text says :n '+req.responseText); } [/code] you could use a third party javascript library like yui from yahoo ,which would js need you to have two files yahoo.js and connection.js .
@bosky101Mar 03.2007 — #AJAX is JS and XML, not just JS. Thus the reason it stands for:
[b]A[/b]synchronous
[b]J[/b]avaScript
[b]A[/b]nd
[b]X[/b]ML[/QUOTE]
hmmm ,well looks like i'll have to bring in a bit of history here regarding the X in ajax ; just to make things clear .
When Alex Hopmann of microsoft first created this new programming model , he needed to pack what he created with IE ,and the xml group was the group which he chose . I quote from his blog I realized that the MSXML library shipped with IE and I had some good contacts over in the XML team who would probably help out- I got in touch with Jean Paoli who was running that team at the time and we pretty quickly struck a deal to ship the thing as part of the MSXML library. [b]Which is the real explanation of where the name XMLHTTP comes from[/b]- the thing is mostly about HTTP and [b]doesn't have any specific tie to XML other than that was the easiest excuse for shipping it so I needed to cram XML into the name[/b] (plus- XML was the hot technology at the time and it seemed like some good marketing for the component).[/quote]
ofcourse the format of the response of an ajax call , can be either responseText or responseXML ,but heres how the name came into being though.
check out [url=http://www.alexhopmann.com/xmlhttp.htm]the article[/url] for yourself ,its a great read ?
@FangMar 03.2007 — #When Alex Hopmann of microsoft first created this new programming model , he needed to pack what he created with IE ,and the xml group was the group which he chose .[/QUOTE] I think you are forgetting Scott Isaacs
@AmazingAntauthorMar 03.2007 — #Well, it got stuck on the same problem again, so I decided to directly copy the code from your last post into the file, and it seems that somehow a space was missing right in front of callBackFN.:eek: that'd be my bad for deleting it.?
Once that space was back and it said callBackFN, it worked fine, except... when the page loads, it gives 4 pop-ups, instead of just one. The first two don't have the right text, just "the text says :", but the second two have the correct text in them in addition to "the text says :".
So thanks for getting me this far, but one last thing. When I try to assign the text to a variable, and use the new variable in another alert, the alert becomes the second pop-up, making a grand total of 5. It says whatever static text I have in there, with "undefined" as the value of the variable.?
@AmazingAntauthorMar 04.2007 — #Well, I'm going to opt out of having a loading message for now.
The code change you gave me fixed it to the point of only giving me one pop-up, but my variable was still coming out as undefined.
With a little bit of work, I figured out the problem. My variable is going to suck at life unless I do what I plan to do with it inside the function readDone.
Little bit bothersome, but I can live with it no prob.
In the meantime, thanks for helping me work this out!
Edit: My web server sucks right now because my entire computer is bogged down...? :eek: :mad: