最近遇到需要頁面自動重新整理的功能,於是乎就想到了Iframe……
Iframe在單個頁面中可以正常的實現自動重新整理
舉一個關於不斷往a.txt中寫資訊的小例子:
all.cgi頁面的代碼
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<title>all device</title>
</head>
<body>
<iframe id='myIfm' src='fifo.cgi' width='150' height='150'></iframe>
</body></html>
int fifo;
if ((fifo = open("a.txt", O_WRONLY | O_APPEND)) < 0) //判斷開啟a.txt檔案是否成功
{
printf("open error: %s/n", strerror(errno)); //錯誤列印錯誤資訊,返回
return(0);
}
write(fifo,"bbb/n",4); //正確的時候就不斷的往a.txt檔案中寫“bbb”
運行一下,成功……
但是在需要用button提交的時候只能執行一次,這個問題迷糊了我3天,終於把它弄明白了,分享一下,希望可以幫到和我一樣遭遇的朋友……
舉一個關於用按鈕提交使頁面不斷往a.txt中發送資訊的小例子:
<script javascript="javascript">
function ok()
{
var myIfm=document.all("myIfm").src="fifo.cgi"; //這個調用的src可以是html,cgi,c……任何形式的,根據自己的需求而定
window.setTimeout( "ok2()",1000); //需要用到這句(一定要用)是因為載入另一個頁面的時候需要時間,
//只能等到第一個頁面載入完畢後才能調用
}
function ok2(){
//alert("ddd");
document.all( "myIfm").contentWindow.location="fifo.cgi";
}
</script>
all.cgi頁面的代碼
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<title>all device</title>
</head>
<body>
<input type='submit' name='btnOpen' value='Open' onclick="javascript:ok()"/>
<iframe id='myIfm' src='' width='150' height='150'></iframe>
</body></html>
fifo.cgi頁面的代碼
int fifo;
if ((fifo = open("a.txt", O_WRONLY | O_APPEND)) < 0) //判斷開啟a.txt檔案是否成功
{
printf("open error: %s/n", strerror(errno)); //錯誤列印錯誤資訊,返回
return(0);
}
write(fifo,"bbb/n",4); //正確的時候就不斷的往a.txt檔案中寫“bbb”
運行一下,成功……
到此,大功告成嘍,沒白忙活一陣子,終於弄出來了,很是興奮啊……
希望對某些和我遇到過一樣的問題的朋友有所協助……