Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text From File 1

Status
Not open for further replies.

dakoz

Programmer
Feb 2, 2002
74
How can i retrieve a text file & put it in my html code?

i want all my news & reports 2 be updated in that .txt file
so i wont have to change my index.html page each time i want 2 update
 
Hey there,

there's a number of ways, with a simple javascript, adding the text with <object> tags but probably the best and very easy way is with SSI.

Put your news and reposts into a text or html file, I think you should include the html tags with your news text.

Then place the following into your index page where you want the news to appear.

if in same directory
<!--#include file=&quot;news.txt&quot; -->

in different directory
<!--#include virtual=&quot;news.txt&quot; -->

You will probably have to change the extensions of your html pahes to .shtml

Just update the txt file and it will automatically show on all pages where you have put the SSI code

Should work nicely...



É

endamcg-logo1b.gif

 
HTML & JavaScript are client side. You'll have to use a server side capability, like perl of PHP to do that. There are some good sites where you can get free perl scrips, try worldwidemart.com for a start.
 
You cannot access .txt files using javascript.
However, you can do something similar using .js file (used for external javascript code attached to a webpage).

Here's an example how to do it:

news.js:
--------

var news = &quot;Write your news here&quot;;
var report = &quot;Write a report here&quot;;

function writeNews() {
document.write('<div style=&quot;font bold 12px sans-serif; color:green&quot;>', news, '</div>')
}

function writeReport() {
document.write('<div style=&quot;font bold 10px sans-serif; color:eek:range&quot;>', report , '</div>')
}

--------
(don't add any tags like <script> to the news.js file!)


Your main page:

<html>
<head>
<script language=&quot;javascript&quot; src=&quot;news.js&quot;></script>
</head>
<body>
Current news:<br>
<script>writeNews();</script>
<br><br>
Reports:<br>
<script>writeReport();</script>
</body>
</html>

 
Hi y2k1981,

maybe I misunderstood the problem, but why cant you use javascript?

news.js::
function writenews() {
document.write('Latest news::&quot;>');
document.write('Date: news text');
}

In the html document::
<script language=&quot;JavaScript&quot;>
writenews();
</script>

Ok, its not really a good way to do it but just to show how i mean it can be done. I have seen similar on many sites.

I still think SSI would be best instead of messing with php or perl. IMHO


É

endamcg-logo1b.gif

 
You cannot access .txt files using javascript.

Why not? Maybe this isn't quite &quot;javascript&quot;, but this works:
<script src=&quot;filename.txt&quot;></script>

Rick if(($question==&quot;has been bugging me&quot;
AND $answer==&quot;fixed the problem&quot;) OR $answer==&quot;really good post&quot;){
print(&quot;Star&quot;);
}else{
print(&quot;Thanks.&quot;);
}
 
You cannot access .txt files using javascript.

Why not? This works for me:

<script>
<!--
function upd(){
getElementById('whatever').innerHTML='<script src=&quot;news.txt&quot;></script>
';
}
//-->
</script>
<div id=&quot;whatever&quot;>
<script src=&quot;news.txt&quot;></script>
</div>
<a href=&quot;javascript:upd();&quot;>Update news</a>

Rick if(($question==&quot;has been bugging me&quot;
AND $answer==&quot;fixed the problem&quot;) OR $answer==&quot;really good post&quot;){
print(&quot;Star&quot;);
}else{
print(&quot;Thanks.&quot;);
}
 

>> getElementById('whatever').innerHTML='<script src=&quot;news.txt&quot;></script>

It's the same idea as using external .js file, but using invalid file format.

 
I (learning this a long time ago lots of other users on here) have used this before and it's worked fine. :-/

Rick if(($question==&quot;has been bugging me&quot;
AND $answer==&quot;fixed the problem&quot;) OR $answer==&quot;really good post&quot;){
print(&quot;Star&quot;);
}else{
print(&quot;Thanks.&quot;);
}
 
...learning this from other users...It's just one word; who cares? :-D

Rick if(($question==&quot;has been bugging me&quot;
AND $answer==&quot;fixed the problem&quot;) OR $answer==&quot;really good post&quot;){
print(&quot;Star&quot;);
}else{
print(&quot;Thanks.&quot;);
}
 
Hmm Rick I tried your method and no worky for me am I missing something in the news.txt file? could you post a sample news .text file please? If I can get it working it would help me alot because I can get people to write thier own anouncements with out needing any formatting.
 
I don't think there needs to be anything special in the .txt file from Rick's example! Just whatever basic presentation tags you require!

If ya can't get it to work try one of the other ways.
As far as updating the news goes, there's no difference between updating a .js file and a .txt file! Try it with .js also.


É

endamcg-logo1b.gif

 
anywayzZz &quot;starway&quot;'s sollution worked 4 me...

thanx a lot.
 
!!HELP!!

RISTMO - Your script is exactly what I need & wil enable me to keep eye on certain Unix functions from web. i.e. Unix updates text file and via web I can see the contents being updated in real time (without having to refresh the browser each time). Perfect, apart from I cannot get it to re-read the file. No error, but blank result.

First time my file news.txt contains document.write(&quot;123&quot;). This displays perfectly. When I click on update, my file news.txt contains document.write(&quot;456&quot;). I get blank / nothing displayed. What am I doing wrong?

Function works because if I uncomment the lines with //, the whole thing works fine (but kinda defeats object of reading from text file).

function upd()
{
document.getElementById(&quot;updte&quot;).innerHTML='<SCRIPT SRC=&quot;c:\search\cgi-bin\news.txt&quot;></script>';
//hits += 1;
//document.getElementById(&quot;updte&quot;).innerHTML=&quot;Number of clicks = &quot; + hits;
}
//-->
</script>

<div id=&quot;updte&quot;>
<script src=&quot;c:\search\cgi-bin\news.txt&quot;></script>
</div>
<a href=&quot;javascript:upd();&quot;>Update news</a>
 
I have a free program, HTMLKISS, on my website that will convert your text file to HTML, maintain blank lines and create and anchor headings.

I have found this site so useful that I will provide a free upgrade to the, more fully featured, personal version for anyone who requests it and who identifies themselves as a tek-tips user. Clive
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top