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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying contents of text file

Status
Not open for further replies.

rogerte

Programmer
Nov 9, 2001
164
GB
Now that Twitter has changed their API we can't use that medium to create "news items" for our website.

So rather than have to update HTML pages I thought we would go down the "upload simple text file and let javascript take care of it" route.

I threw together a javascript file (news.js) containing a piece of code: HTML
JavaScript:
newsArray = new Array(

"Title1",
"News Item 1 description text.",

"Title2",
"News Item 2 description text.",

"Title3",
"News Item 3 description text.",

"Title4",
"News Item 4 description text.",

"", "");


function printnewsitems(){
  var news_str = "";
  for(i=0; i<(newsArray.length-2); i+=2){
    news_str += "<b>" + newsArray[i] + "</b><br>" + newsArray[i+1] + "<br><br>";
  }

  if (document.all){
    document.all['newsitems'].innerHTML = news_str;
  }
  else{
    var a_n_other= document.getElementById("newsitems");
    a_n_other.innerHTML = news_str;  
  }
}

In the HTML page I have added:
<script type="text/javascript" src="news.js"></script>
to the header section.

In the body I have added
<DIV id="newsitems" width="100%"></DIV>
which is where I want the news items to appear

After the </BODY> tag I have added
<SCRIPT language="JavaScript">
printnewsitems();
</SCRIPT>

I uploaded the test page and js file.

Page loads as expected, but the news items do not display.

Any ideas gratefully received!

Thanks
 
Your code as is works for me. What browser are you testing this on?

Do you get any errors? Most browsers have ways of showing JS errors.

Other than that, your array structure is horrible, but that can be fixed.

Try adding alerts to your code, so you know its getting executed.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
It seems to be the same for all browsers I have tested it on (Firefox 21, IE9 and Opera) - just get the page title and nothing else.

Here is the HTML page:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <META NAME="ROBOTS" CONTENT="INDEX,NOFOLLOW">
  <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
  <title>Test News</title>
    
  <script type="text/javascript" src="scripts/newsitems.js"></script>
  
  </head>
  <body>
      <div>
        <p>News Test</p>
      </div>
 
      <div id="newsitems"> 
      </div>
  </body>
  
   <script type="text/javascript">
   printnewsitems();
   </script>

</html>

Now I will admit that I have never written anyJavascript before, and my attempt is based on quick reading of a few on-line tutorials!

 
The next question then is where exactly is your newsitems.js. According to your code, it should be in a folder named scripts that is at the same level as your HTML document.

If you are using Firefox, go to the Tools Menu->Web Developer->Error Console. This should show any Js errors you might have. You'll want to clear the errors and refresh your page so you get only the errors for that particular page.

If you have any errors, you can post them here.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Sorry for delay in getting back to you, sorted out a slightly different way of doing it.

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top