JavaScript to Fetch Text

R

Roy Schestowitz

I would like to retrive some raw information (text) from one machine and
display it within a page in another. I was thinking about using JavaScript
that will fetch the text from a given URL and add it to the body. Does
someone know a page or example which I can re-use? I hope I explained this
clearly enough. If not, follow up with question.

Many thanks to all who can/will help,

Roy
 
N

Neredbojias

With neither quill nor qualm, Roy Schestowitz quothed:
I would like to retrive some raw information (text) from one machine and
display it within a page in another. I was thinking about using JavaScript
that will fetch the text from a given URL and add it to the body. Does
someone know a page or example which I can re-use? I hope I explained this
clearly enough. If not, follow up with question.

Check out the following link. It does what _I think_ you want. If you
need more info beyond the html, I'll be here.

http://www.neredbojias.com/alpha/jokes.html
 
N

Neredbojias

With neither quill nor qualm, Neredbojias quothed:
With neither quill nor qualm, Roy Schestowitz quothed:


Check out the following link. It does what _I think_ you want. If you
need more info beyond the html, I'll be here.

http://www.neredbojias.com/alpha/jokes.html

Oops, check that. It does it in PHP, no javascript needed. Originally
it was javascript but I must have changed it before I went senile.

You can use javascript to do the same thing, -just pick-off the
search/query strings with js instead of PHP and make a .js file instead
of a text file with variablized text.
 
R

Roy Schestowitz

__/ [Neredbojias] on Monday 12 September 2005 05:44 \__
With neither quill nor qualm, Neredbojias quothed:


Thank you for the link. I have had a quick look (nice design by the way).

Oops, check that. It does it in PHP, no javascript needed. Originally
it was javascript but I must have changed it before I went senile.

You can use javascript to do the same thing, -just pick-off the
search/query strings with js instead of PHP and make a .js file instead
of a text file with variablized text.


The problem is that PHP, being server-side does not expose the code. I was
hoping for a quick grab of JavaScript code. Also, renaming the page/s,
namely having the html suffix changed to php, will require modification of
internal links, or even worse, external inbound links. Redirections would
be an overkill.

To rephrase my request as you were not sure what I meant, I seek to grab
text from http://example.org/data.txt and then embed it in a <span>, for
instance, in http://example.com/page.html. data.txt gets updated all the
time, hence it needs to be grabbed. If JavaScript was disabled, there would
be no harm either.

Thanks again,

Roy
 
V

Vladdy

Roy said:
__/ [Neredbojias] on Monday 12 September 2005 05:44 \__

With neither quill nor qualm, Neredbojias quothed:



Thank you for the link. I have had a quick look (nice design by the way).


Oops, check that. It does it in PHP, no javascript needed. Originally
it was javascript but I must have changed it before I went senile.

You can use javascript to do the same thing, -just pick-off the
search/query strings with js instead of PHP and make a .js file instead
of a text file with variablized text.



The problem is that PHP, being server-side does not expose the code. I was
hoping for a quick grab of JavaScript code. Also, renaming the page/s,
namely having the html suffix changed to php, will require modification of
internal links, or even worse, external inbound links. Redirections would
be an overkill.

To rephrase my request as you were not sure what I meant, I seek to grab
text from http://example.org/data.txt and then embed it in a <span>, for
instance, in http://example.com/page.html. data.txt gets updated all the
time, hence it needs to be grabbed. If JavaScript was disabled, there would
be no harm either.

Thanks again,

Roy
function getFile(filename)
{ oxmlhttp = null;
try
{ oxmlhttp = new XMLHttpRequest();
oxmlhttp.overrideMimeType("text/xml");
}
catch(e)
{ try
{ oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{ return null;
}
}
if(!oxmlhttp) return null;
try
{ oxmlhttp.open("GET",filename,false);
oxmlhttp.send(null);
}
catch(e)
{ return null;
}
return oxmlhttp.responseText;
}

function returns file content as text string.
beware of x-domain scripting restrictions.
 
R

Roy Schestowitz

__/ [Toby Inkster] on Monday 12 September 2005 07:22 \__
Google: AJAX?

It would be an overkill. What I try to do is rather simple. Thank you for
the well-intended suggestion, Toby.

Roy
 
R

Roy Schestowitz

__/ [Vladdy] on Monday 12 September 2005 07:51 \__
Roy said:
__/ [Neredbojias] on Monday 12 September 2005 05:44 \__

<SNIP>

To rephrase my request as you were not sure what I meant, I seek to grab
text from http://example.org/data.txt and then embed it in a <span>, for
instance, in http://example.com/page.html. data.txt gets updated all the
time, hence it needs to be grabbed. If JavaScript was disabled, there
would be no harm either.

Thanks again,

Roy
function getFile(filename)
{ oxmlhttp = null;
try
{ oxmlhttp = new XMLHttpRequest();
oxmlhttp.overrideMimeType("text/xml");
}
catch(e)
{ try
{ oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{ return null;
}
}
if(!oxmlhttp) return null;
try
{ oxmlhttp.open("GET",filename,false);
oxmlhttp.send(null);
}
catch(e)
{ return null;
}
return oxmlhttp.responseText;
}

function returns file content as text string.
beware of x-domain scripting restrictions.


Thank you very much for the reply.

Below is the code that I have added as a wrapper. I still have a slight
problem, which perhaps you can solve 'on the spot'. The following
simplified example will work fine (I do not know JS methods):

Code:
function showInfo()
{
oxmlhttp =getFile('/robots.txt');
document.write(oxmlhttp);
}

but the following will not


Code:
function showInfo()
{
oxmlhttp =getFile('http://example.org/robots.txt');
document.write(oxmlhttp);
}

Is there a simple way around this? The text remains 'null' if I try the
latter, which is why I need to access a machine of mine from the Web
server.

Thanks again,

Roy
 
N

Neredbojias

With neither quill nor qualm, Roy Schestowitz quothed:
To rephrase my request as you were not sure what I meant, I seek to grab
text from http://example.org/data.txt and then embed it in a <span>, for
instance, in http://example.com/page.html. data.txt gets updated all the
time, hence it needs to be grabbed. If JavaScript was disabled, there would
be no harm either.

The "grabbing the text" part is the difficulty, especially if it's
dynamic. I know of no way to "address" plain text (files) with simple
javascript (from another source). What I've done in the past is put my
text, which is static, in a .js file and call it from a regular html
page. The text itself had to be loaded into variables inside the .js
file so it could be manipulated from within the html page. You might
say it was a "poor man's include".

This doesn't sound like what you want but you should be able to accept
that if you've ever been married.
 
R

Roy Schestowitz

__/ [Neredbojias] on Monday 12 September 2005 18:23 \__
With neither quill nor qualm, Roy Schestowitz quothed:


The "grabbing the text" part is the difficulty, especially if it's
dynamic. I know of no way to "address" plain text (files) with simple
javascript (from another source). What I've done in the past is put my
text, which is static, in a .js file and call it from a regular html
page. The text itself had to be loaded into variables inside the .js
file so it could be manipulated from within the html page. You might
say it was a "poor man's include".


I thought of simpler solutions, but I am afraid there are none. I output
text to a file every 10 minutes and I want to send it to the Web server.
More easily, I am able to make the output public and fetch it from the Web
server to be included (dynamically) as part of a file that resides on the
server. So essentially I want to mix data from two computers to make up a
single page. I can do that with images (HotLinking), but not with text,
yet.

This doesn't sound like what you want but you should be able to accept
that if you've ever been married.


*LOL* I began to lose interest in that file grabbing idea. It appears to be
more complex than I initially speculated (one of those silly ideas that
crop up while walking in the street). Let's wait and see if Vlad can come
up with an easy fix, exchanging file with a URL that it remote.

Roy
 
V

Vladdy

Roy said:
__/ [Vladdy] on Monday 12 September 2005 07:51 \__

Roy said:
__/ [Neredbojias] on Monday 12 September 2005 05:44 \__

<SNIP>

To rephrase my request as you were not sure what I meant, I seek to grab
text from http://example.org/data.txt and then embed it in a <span>, for
instance, in http://example.com/page.html. data.txt gets updated all the
time, hence it needs to be grabbed. If JavaScript was disabled, there
would be no harm either.

Thanks again,

Roy

function getFile(filename)
{ oxmlhttp = null;
try
{ oxmlhttp = new XMLHttpRequest();
oxmlhttp.overrideMimeType("text/xml");
}
catch(e)
{ try
{ oxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{ return null;
}
}
if(!oxmlhttp) return null;
try
{ oxmlhttp.open("GET",filename,false);
oxmlhttp.send(null);
}
catch(e)
{ return null;
}
return oxmlhttp.responseText;
}

function returns file content as text string.
beware of x-domain scripting restrictions.



Thank you very much for the reply.

Below is the code that I have added as a wrapper. I still have a slight
problem, which perhaps you can solve 'on the spot'. The following
simplified example will work fine (I do not know JS methods):

Code:
function showInfo()
{
oxmlhttp =getFile('/robots.txt');
document.write(oxmlhttp);
}

but the following will not


Code:
function showInfo()
{
oxmlhttp =getFile('http://example.org/robots.txt');
document.write(oxmlhttp);
}

Is there a simple way around this? The text remains 'null' if I try the
latter, which is why I need to access a machine of mine from the Web
server.

Thanks again,

Roy
As I mentioned in my original post, the xmlhttp file access only works
within the same domain - security reasons. In the first example, the
same domain is assured by using the relative URL, in the second one, it
may not be (for example, you may need to add "www." to match the domain
of the page containing script; or it not work at all with the full
domain name - I have never tried it for the aformentioned reason)
 
N

Neredbojias

With neither quill nor qualm, Roy Schestowitz quothed:
__/ [Neredbojias] on Monday 12 September 2005 18:23 \__
With neither quill nor qualm, Roy Schestowitz quothed:


The "grabbing the text" part is the difficulty, especially if it's
dynamic. I know of no way to "address" plain text (files) with simple
javascript (from another source). What I've done in the past is put my
text, which is static, in a .js file and call it from a regular html
page. The text itself had to be loaded into variables inside the .js
file so it could be manipulated from within the html page. You might
say it was a "poor man's include".


I thought of simpler solutions, but I am afraid there are none. I output
text to a file every 10 minutes and I want to send it to the Web server.
More easily, I am able to make the output public and fetch it from the Web
server to be included (dynamically) as part of a file that resides on the
server. So essentially I want to mix data from two computers to make up a
single page. I can do that with images (HotLinking), but not with text,
yet.

This doesn't sound like what you want but you should be able to accept
that if you've ever been married.


*LOL* I began to lose interest in that file grabbing idea. It appears to be
more complex than I initially speculated (one of those silly ideas that
crop up while walking in the street). Let's wait and see if Vlad can come
up with an easy fix, exchanging file with a URL that it remote.

I think you're going to end up needing Perl (or Java?) Security is an
issue. Anyway, best of luck.
 
R

Roy Schestowitz

__/ [Vladdy] on Tuesday 13 September 2005 00:14 \__
Roy said:
<SNIP

Code:
function showInfo()
{
oxmlhttp =getFile('/robots.txt');
document.write(oxmlhttp);
}

but the following will not


Code:
function showInfo()
{
oxmlhttp =getFile('http://example.org/robots.txt');
document.write(oxmlhttp);
}

Is there a simple way around this? The text remains 'null' if I try the
latter, which is why I need to access a machine of mine from the Web
server.

Thanks again,

Roy

As I mentioned in my original post, the xmlhttp file access only works
within the same domain - security reasons. In the first example, the
same domain is assured by using the relative URL, in the second one, it
may not be (for example, you may need to add "www." to match the domain
of the page containing script; or it not work at all with the full
domain name - I have never tried it for the aformentioned reason)

That's what I was worried about. It apparently discourages scraping, which
in this case is something I want to do benevolently -- grabbing content
from my own machine. I can think of alternative solutions involving a
grabber like wget, but that would take me too far.

Many thanks,

Roy
 
R

Roy Schestowitz

__/ [Neredbojias] on Tuesday 13 September 2005 01:56 \__
With neither quill nor qualm, Roy Schestowitz quothed:
__/ [Neredbojias] on Monday 12 September 2005 18:23 \__
With neither quill nor qualm, Roy Schestowitz quothed:

To rephrase my request as you were not sure what I meant, I seek to
grab text from http://example.org/data.txt and then embed it in a
<span>, for instance, in http://example.com/page.html. data.txt gets
updated all the time, hence it needs to be grabbed. If JavaScript was
disabled, there would be no harm either.

The "grabbing the text" part is the difficulty, especially if it's
dynamic. I know of no way to "address" plain text (files) with simple
javascript (from another source). What I've done in the past is put my
text, which is static, in a .js file and call it from a regular html
page. The text itself had to be loaded into variables inside the .js
file so it could be manipulated from within the html page. You might
say it was a "poor man's include".


I thought of simpler solutions, but I am afraid there are none. I output
text to a file every 10 minutes and I want to send it to the Web server.
More easily, I am able to make the output public and fetch it from the
Web server to be included (dynamically) as part of a file that resides on
the server. So essentially I want to mix data from two computers to make
up a single page. I can do that with images (HotLinking), but not with
text, yet.

This doesn't sound like what you want but you should be able to accept
that if you've ever been married.


*LOL* I began to lose interest in that file grabbing idea. It appears to
be more complex than I initially speculated (one of those silly ideas
that crop up while walking in the street). Let's wait and see if Vlad can
come up with an easy fix, exchanging file with a URL that it remote.

I think you're going to end up needing Perl (or Java?) Security is an
issue. Anyway, best of luck.

I have given up at this stage. Test file is now in the wastebasket...
*frown*

Thanks for the help,

Roy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top