How to reload an IFrame every x seconds?

2019-08-14 05:16发布

Was wondering how I can reload an iframe every x seconds, perferably not using javascript.

Thanks.

1条回答
The star\"
2楼-- · 2019-08-14 05:44

With a Refresh: x HTTP header or with an HTML element in the document loaded into the iframe:

<meta http-equiv="refresh" content="x" />

This element should be placed inside of the document's <head/> element.

If you do not have control over the document loaded into the frame or the server that it is served from, you have two options:

  1. JavaScript.
  2. Write another HTML page with the above <meta/> element and include an iframe in that page targeting the other page. So you will have an iframe inside an iframe: outer document -> iframe(inner document with meta-refresh) -> iframe(original iframe target)

EDIT: Regarding option #2, here's a decent generic iframe in PHP that gives some flexibility in terms of refresh time and style. Just call it with something like:

http://www.mydomain.com/genericIframe.php?url=http://my.domain.com/mypage.htm&refreshTime=60&style=putYourStyleAttribHere

Here's the PHP/HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Generic Iframe</title>
    <meta http-equiv="refresh" content="<?php print $_REQUEST['refreshTime']; ?>" />
</head>
<body>
    <iframe src="<?php print $_REQUEST['url']; ?>" style="<?php print $_REQUEST['style']; ?>"></iframe>
</body>
</html>
查看更多
登录 后发表回答