/    Sign up×
Community /Pin to ProfileBookmark

JavaScript: Retrieve text from URL

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.

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@felgallMar 03.2007 — Use AJAX.
Copy linkTweet thisAlerts:
@AmazingAntauthorMar 03.2007 — So would that mean that it is not possible with [I]just[/I] JS?
Copy linkTweet thisAlerts:
@bosky101Mar 03.2007 — ajax is completely JS !

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 .
Copy linkTweet thisAlerts:
@konithomimoMar 03.2007 — ajax is completely JS ![/QUOTE]

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
Copy linkTweet thisAlerts:
@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 ?

Keep Clicking,

Bosky
Copy linkTweet thisAlerts:
@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
Copy linkTweet thisAlerts:
@bosky101Mar 03.2007 — ^ ok . will chk it out 8 )
Copy linkTweet thisAlerts:
@AmazingAntauthorMar 03.2007 — Ok, ok. Whatever. Point is, this can be done.

However, I found a few problems with the code there, fixed them, and now I'm stuck getting the error "object expected". The problem is right here:

xmlHttp.onreadystatechange = function() {[COLOR="Red"][SIZE="4"]|[/SIZE][/COLOR]callback(xmlHttp); }

In addition to that, Firefox wants me to know that the function Callback isn't defined, which seems like it's coming from the same problem.

What might need to be changed?
Copy linkTweet thisAlerts:
@bosky101Mar 03.2007 — ah ha !

small typo again .

change

xmlHttp.onreadystatechange = function() { [b]callback[/B](xmlHttp); }

to

xmlHttp.onreadystatechange = function() { [b]callBackFn[/b](xmlHttp); }



ill correct the previous post as well for future reference .
Copy linkTweet thisAlerts:
@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.?

How might I get that fixed?
Copy linkTweet thisAlerts:
@bosky101Mar 04.2007 — replace [code=php]xmlHttp.onreadystatechange = function() { callBackFn(xmlHttp); } [/code] with [code=php]
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState==4 && xmlHttp.status==200) {
//all done !
callBackFn(xmlHttp);
}else{
//display a 'loading...' message if you want...
}
} [/code]
Copy linkTweet thisAlerts:
@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:

I'm still off to reboot.

Thanks again!
×

Success!

Help @AmazingAnt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...