Reloading the block content

Reloading the block content

by Zoran Jeremic -
Number of replies: 10
Hi,

Can you suggest me how to make my block to refresh its content on a certain period of time, without reloading the page that the block is contained in?

Thanks,
Zoran
Average of ratings: -
In reply to Zoran Jeremic

Re: Reloading the block content

by Tim Hunt -
Core developers இன் படம் Documentation writers இன் படம் Particularly helpful Moodlers இன் படம் Peer reviewers இன் படம் Plugin developers இன் படம்
The quick and dirty way would be to make your block display an iframe, and make the page displayed in the iframe reload itself periodically.

A more sophisticated solution would use ajax-y techniques.
In reply to Tim Hunt

Re: Reloading the block content

by Zoran Jeremic -

Tim,

I have tried the first solution you suggested me, with iframe. I have tried many different javascripts to reload the iframe but I couldn't make it works. This one bellow reloads the whole page instead of the iframe only. I've put and activate this script from the iframe.

<script language="JavaScript">
<!--
var sURL = unescape(window.frames['my_iframe'].location.pathname);
function doLoad()
{
setTimeout( "refresh()", 5*1000 );
}
function refresh()
{
window.location.href = sURL;
}
//-->
</script>

Can you suggest me what is wrong here?

Thanks

In reply to Zoran Jeremic

Re: Reloading the block content

by Tim Hunt -
Core developers இன் படம் Documentation writers இன் படம் Particularly helpful Moodlers இன் படம் Peer reviewers இன் படம் Plugin developers இன் படம்
I think you need to do something like window.frames['my_iframe'].location.href = sURL.
In reply to Tim Hunt

Re: Reloading the block content

by Zoran Jeremic -
I have tried this and many other options and it does not work. In this case I got error on page "Error: 'window.frames.my_iframe.location' is null or not an object".
If the window is the main window (course page), do I have to access the block object before trying to reach my frame included in block? Where should I put JavaScript code to access this iframe?
In reply to Zoran Jeremic

Re: Reloading the block content

by Dale Davies -
If you are using a meta refresh in the head section of the iframe source then you dont need any javascript.

The iframe source being the content of your iframe, not the page/block containing the iframe itself.

When the iframe source file is loaded, the meta refresh will refresh that at the interval specified in the meta content (in the case of my previous example, every 5 seconds). But it should only refresh the content of the iframe, not the page containing it.

Argh, hard to explain!
In reply to Dale Davies

Re: Reloading the block content

by Zoran Jeremic -
Hi Dale,

That was the first thing I have tried. I have tried it again now, but it refresh the whole page again. I have tried to put
echo '<META HTTP-EQUIV="Refresh" content="5" url="includedContent.php">';

at the top of the included page, as well as in the block page (before block class and in the function get_content), and nothing again.

This sounds as a good solution, but it doesn't works for me, or I do it on the wrong way.
In reply to Zoran Jeremic

Re: Reloading the block content

by Dale Davies -
Well, Ive not tried it out, but this is what I was thinking of.

Your block code might look like this....

<?php
class block_testblock extends block_base {

function init() {
$this->title = 'Test Block';
$this->version = 2007101509;
}

function get_content() {
global $CFG;
if ($this->content !== NULL) {
return $this->content;
}

$this->content = new stdClass;
$this->content->text = '<iframe src ="block_content.php"></iframe>';
$this->content->footer = '';

return $this->content;
}
}
?>


And the "block_content.php" would contain something like this....

<?php
echo '<head><meta http-equiv="refresh" content="5" /></head>';
// After here do your magic //
?>


My apologies if it doesnt actually work.
In reply to Dale Davies

Re: Reloading the block content

by Zoran Jeremic -
Yes, that is exactly what I have done. I have also thought that it should work that way, but it doesn't. Maybe because the function get_content() returns $this->content object and makes it to be the part of the whole page.

In reply to Zoran Jeremic

Re: Reloading the block content

by Dale Davies -
Within the iframe could you use a meta refresh?

In the html/php file that is the source for your iframe, put something like this at the top...

<head><meta http-equiv="refresh" content="5"/></head>

The above will refresh the page every 5 seconds, but should only refresh the page that is viewed through the iframe, not the page the iframe resides on. Ive used this in the past, it might work for you.

One thing though, its good practice to allow the user to change the refresh time, or to turn off the automatic refresh entirely.

I will admit, this technique (iframe/meta refresh) is not as nice as whatever you could do with some ajax, but it is quite simple in comparison.