当前页面: 开发资料首页 → Javascript 专题 → JavaScript实现自动刷新页面
摘要: JavaScript实现自动刷新页面
今天访问了硅谷动力网络学院,发现其中下部有一行图片经过指定时间变化一次,实现图片文章的显示,觉得效果不错,打开查看了一下源代码,发现其使用的是刷新的功能,在这里给大家说说。具体代码为:
<table width="748" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height=95 background="ab28-1.gif" width="23" > </td>
<td height=95 align="center" background="ab28-2.gif">
</td>
<td height=95 background="ab28-3.gif" width="16"></td>
</tr>
</table>
<script>setTimeout("location.href='rdztt2.html'",10000);</script>
把上面的代码保存为一个文件为rdztt1.html,然后我们再建立了一个文件rdztt2.html,内容如下:
<table width="748" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height=95 background="ab28-1.gif" width="23" > </td>
<td height=95 align="center" background="ab28-2.gif">
</td>
<td height=95 background="ab28-3.gif" width="16"></td>
</tr>
</table>
<script>setTimeout("location.href='rdztt3.html'",10000);</script>
然后又建立了一个rdztt3.html,内容如下:
<table width="748" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height=95 background="ab28-1.gif" width="23" > </td>
<td height=95 align="center" background="ab28-2.gif">
</td>
<td height=95 background="ab28-3.gif" width="16"></td>
</tr>
</table>
<script>setTimeout("location.href='rdztt1.html'",10000);</script>
大家可以注意到上面三个文件中均有如下代码:
<script>setTimeout("location.href='*****.html'",10000);</script>
这就是实现自动刷新的代码,使用了setTimeout()函数设置为每隔10秒钟刷新一次!最后使用iframe标签加到主页中:
<iframe name="titleframe" src='/eschool/includes/gdtup/tu5/rdztt1.html' noresize="Yes" scrolling="No" frameborder="0" marginheight="0" marginwidth="0" width=750 height=100></iframe>
具体演示效果大家可以看看
http://www.enet.com.cn/eschool/中下部变化的图片
http://www.enet.com.cn/eschool/includes/gdtup/tu5/rdztt1.html实际显示的页面
</td> </tr> </table>