# JavaScript: How to load external text database to webpage at regular interval?

**URL:** <https://forum.kirupa.com/t/javascript-how-to-load-external-text-database-to-webpage-at-regular-interval/266880>\
**Category:** web dev\
**Created:** [July 25, 2008, 2:52am UTC](https://forum.kirupa.com/t/javascript-how-to-load-external-text-database-to-webpage-at-regular-interval/266880 "2008-07-25T02:52:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex298](https://avatars.discourse-cdn.com/v4/letter/a/76d3ee/32.png) [@alex298](https://forum.kirupa.com/u/alex298)\
**Post date:** [July 25, 2008, 2:52am UTC](https://forum.kirupa.com/t/javascript-how-to-load-external-text-database-to-webpage-at-regular-interval/266880/1 "2008-07-25T02:52:42Z")

</div>

Hello,

## I have a webpage (tips.htm) that will show “tips to-day”. There are basically three pages:

1. tips.html
2. tip.php
3. tip\_database.txt (has both Chinese and English Characters)

* * *

**How it works:**  
The tips that show in tips.html are loaded from tip\_database.txt

**What I wish?**  
I wish the tips showing in tips.html refresh every 10 seconds. Therefore my visitors can see a new tip loading from tip\_database.txt in every 10 seconds.

How can I do that?

Here’s the contents of the three files. Actually I obtained the Javascript on the Internet long time ago.

**MAIN FILE: tips.html**

> 

\<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”  
“[http://www.w3.org/TR/html4/loose.dtd](http://www.w3.org/TR/html4/loose.dtd)”\>\<html\>  
\<head\>  
\<title\>Tips Today\</title\>  
\<script type=“text/javascript”\>  
\<!–  
var index = 0;  
var text\_list = ;  
var file\_name = “tip.php?lang=chi”;  
function init()  
{  
if (!xmlHttp)return;  
xmlHttp.open(“POST”,file\_name,true);  
xmlHttp.onreadystatechange=loadFile;  
xmlHttp.send(“”);  
}  
function loadFile()  
{  
if (xmlHttp.readyState == 4) {  
text\_list = xmlHttp.responseText.split(  
/(?:[\r\s]\*  
[\r\s]_){2,}/g ), len = text\_list.length;  
index = Math.floor(Math.random() \* len);  
xmlHttp = null;  
showMessage();  
}  
}  
function showMessage()  
{  
var content = document.getElementById(“content”);  
if (!content) {  
window.setTimeout(showMessage, 100);  
} else {  
content.innerHTML = encodeHTML(text\_list[index]);  
}  
}  
function encodeHTML(str)  
{  
return new String(str).replace(/\</g, ‘\<’).replace(/\>/g, ‘\>’).replace(/&/g, ‘&’).replace(/  
/g, ‘\<br /\>’);  
}  
//–\>  
\</script\>  
\<script type=“text/javascript”\>  
\<!–  
xmlHttp=XMLHttp();  
function XMLHttp()  
{  
try {  
if (window.XMLHttpRequest) {  
var req = new XMLHttpRequest();  
if (req.readyState == null) {  
req.readyState = 1;  
req.addEventListener(“load”, function () {  
req.readyState = 4;  
if (typeof req.onreadystatechange == “function”) req.onreadystatechange();  
}, false);  
}  
return req;  
} else if (window.ActiveXObject) {  
return new ActiveXObject(getControlPrefix()+“.XmlHttp”)  
}  
} catch ( ex ) {}  
}  
function getControlPrefix()  
{  
if (getControlPrefix.prefix) return getControlPrefix.prefix;  
var prefixes = [“MSXML2”, “Microsoft”, “MSXML”, “MSXML3”], o, o2;  
for (var i = 0; i \< prefixes.length; i++) {  
try {  
o = new ActiveXObject(prefixes_ + “.XmlHttp”);  
o2 = new ActiveXObject(prefixes\* + “.XmlDom”);  
return getControlPrefix.prefix = prefixes\*;  
} catch (ex) {};  
}  
throw new Error(“Could not find an installed XML parser”);  
}  
init();  
//–\>  
\</script\>

\<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”\>

\</head\>

\<body\>  
\<table width=“760” border=“0” align=“center” cellpadding=“0” cellspacing=“0” class=“td01 style1”\>  
\<tr\>  
\<td valign=“top”\>  
\<div align=“center” class=“content” id=“content” style=“font-style:normal; font-variant:normal; font-weight:normal; font-size:14px; font-family:Arial, Helvetica, sans-serif; width:382; height:59”\>  
\<b\>text Appear Here\</b\>  
\</div\>  
\</td\>

```
&lt;/tr&gt;	

```

\</table\>  
\</body\>  
\</html\>

**FILE: tip.php**

> 

\<?  
header(‘Content-Type: text/html; charset=utf-8’);  
include(‘tip\_database.txt’);  
?\>

**FILE: tip\_database.txt**

> 

Tips 1: xxxxxxxxxxxxxxxxxxxxxxx

Tips 2: xxxxxxxxxxxxxxxxxxxxxxxx

… More tips here …

Please help.

Thanks and best regards

Alex
