...

View Full Version : Any methods to refresh a div or iframe?



student101
08-05-2008, 08:42 AM
Is there any method to refresh a div or an iframe from a popup window when the popup opens and or closes?

Cheers

fside
08-05-2008, 07:38 PM
"refresh"? You want to reload an iframe, or just alter the content?

You can reload the iframe by setting a new URL to the iframe's "src" property.

You can alter the content by getting a handle/reference to the iframe:



function IFRloc(strIdIframe){
var targFram = top.document.getElementById(strIdIframe);
var r = targFram.contentDocument ? targFram.contentDocument : (targFram.contentWindow ? targFram.contentWindow.document : self.frames[mapLinkTarg].document);
return r;
};


Then using that object that's returned by the function, above, make changes to the document (if it's from the same server as the parent document).

student101
08-05-2008, 08:37 PM
"refresh"? You want to reload an iframe, or just alter the content?
You can reload the iframe by setting a new URL to the iframe's "src" property.
You can alter the content by getting a handle/reference to the iframe:


function IFRloc(strIdIframe){
var targFram = top.document.getElementById(strIdIframe);
var r = targFram.contentDocument ? targFram.contentDocument : (targFram.contentWindow ? targFram.contentWindow.document : self.frames[mapLinkTarg].document);
return r;
};
Then using that object that's returned by the function, above, make changes to the document (if it's from the same server as the parent document).

Refresh.

Wow, that looks good, let me drop my code in with it to see how this could work;


strIdIframe = iframe id; like <iframe id="strIdIframe"></iframe>
contentDocument = parent.php
contentWindow = child.php
mapLinkTarg = what is this one for?

Is this right?

student101
08-05-2008, 10:01 PM
Anyone able to help me decode this code that was posted?

fside
08-05-2008, 11:12 PM
May have copied from the wrong library:



functionIFRcontent(ifrId) {
var ifr = top.document.getElementById(ifrId);
if (ifr){ return ifr.contentDocument ? ifr.contentDocument : (ifr.contentWindow ? ifr.contentWindow.document : self.frames[ifrId].document);
} else {
return null;
}
}


So - var hIFR = IFRcontent("strIdIframe")

And hIFR.getElementById("someid"), gets you inside the iframe from code running outside.

student101
08-05-2008, 11:52 PM
May have copied from the wrong library:

function IFRcontent(ifrId) {
var ifr = top.document.getElementById(ifrId);
if (ifr){ return ifr.contentDocument ? ifr.contentDocument : (ifr.contentWindow ? ifr.contentWindow.document : self.frames[ifrId].document);
} else {
return null;
}
}
So - var hIFR = IFRcontent("strIdIframe")
And hIFR.getElementById("someid"), gets you inside the iframe from code running outside.
Cool thanks! do I post this in the child.php or parent.php?
Also do I use both of them or just the last one you posted and do I change it up to fit my needs as in div ids and so on?

parent.php

<iframe src="about:blank" name="myframeid" width="120" height="120" scrolling="no" frameborder="0" ></iframe>
child.php

<script language="JavaScript" type="text/javascript">
function IFRcontent(ifrId) {
var ifr = top.document.getElementById(ifrId);
if (ifr){ return ifr.contentDocument ? ifr.contentDocument : (ifr.contentWindow ? ifr.contentWindow.document : self.frames[ifrId].document);
} else {
return null;
}
}
</script>
<body onload="IFRcontent('myframeid');">
Am I close or is there something wrong here?

fside
08-06-2008, 04:50 AM
Well, it was assuming you're running code in the 'parent', and accessing the child iframe. The child.php is the one in the iframe, correct?

I also assumed there was more script to it. So this one little function would go in your larger script, whether you have that hard-coded into the 'parent', or whether an external js file.

And just call the function with the id of your iframe, save the reference it returns, and you can access elements inside the iframe, running code from the 'parent'.

I've done this, myself, taking older pages that are loaded into an iframe, and removing the navigation pane and some particular links which shouldn't be clicked if displayed in a frame.

student101
08-06-2008, 08:26 AM
Well, it was assuming you're running code in the 'parent', and accessing the child iframe. The child.php is the one in the iframe, correct?.Yes with the <a href="child.php">child page</a>, but the child.php is not in the iframe - it could be any file.

just call the function with the id of your iframe, save the reference it returns, and you can access elements inside the iframe, running code from the 'parent'.I get this; <body onload="IFRcontent('myframeid');"> this would go in child.php in order to refresh the iframe in parent.php.

==============

Let me try to explain;
I have parent.php that has an iframe (mylist) which has a link to another page.
The link should only appear if there is data for that $ipaddress, hence the refresh of the iframe (mylist).

The iframe (mylist) has this;

<?php $ipaddress = getenv('REMOTE_ADDR');
if ($rswish > 0) { // Show if not empty ?>
<a href="mylist.php?name=<?php echo $ipaddress; ?>My list</a>
<?php } // Show if not empty ?>
To produce rswish;

SELECT *
FROM table
WHERE name = '$ipaddress'


Currently I am forced to refresh the entire page to get this link to show.

I hope this can help you understand what I need?

student101
08-06-2008, 09:52 AM
I may have solved it! YIPPPEEEEEE!!!

In the iframe I add this;
<meta http-equiv="refresh" content="6">
So every 6 seconds the iframe refreshes to see if the data is allowed to be shown.

Why didn't I see this earlier! dam&it!

I see it keeps refreshing even when it's loaded;
Is there a way to do this?

if (parent.frames['myframeid'].src != 'mypage.php'){
- run the refresh script till it does show up then stop refreshing
}

Cheers

fside
08-06-2008, 12:26 PM
That's very different than trying to manipulate the contents of the iframe from the 'parent' document.

But if you need to update the entire page, a constant 5-6 second page reload might not be recommended.

Why do you think you need to do this? If it's some sort of old-style client-pull, are you doing this for fear users won't have javascript enabled?

student101
08-06-2008, 12:39 PM
No, let me try and explain what it is that I am trying to achieve.

In the parent.php i have a two links Add and View
Clicking Add will add a record to the table, by opening a popup and closes it.
Clicking View will view the record that was added, based on the ip address of the user.

The view link must only be available once a record is added.


I was unable to do this from a popup and divs so I resorted to refreshing the entire page when the popup closed which also lost my content that I was viewing.
Then I tried iframes and this worked as it kept my content available and only refreshed the iframe.

I'm not really interested whether users have javascript enabled or not.

Hope that makes sense.

student101
08-06-2008, 05:44 PM
How to make this work?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pl" xml:lang="pl">
<head>
<title>iframe reload</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script type="text/javascript">


//THIS PART BREAKS and doesn't work, how to fix?

//if(window.frames.reloader.src == "http://www.google.com/"){

//END THIS PART BREAKS


// set your interval in milliseconds
var reloadInterval = 3000;
// this will run when the document is fully loaded
function init() {
setTimeout('reload()',reloadInterval);
}
// this reloads the iframe, and triggers the next reload interval
function reload() {
var iframe = document.getElementById('reloader');
if (!iframe) return false;
iframe.src = iframe.src;
setTimeout('reload()',reloadInterval);
}
//}
// load the init() function when the page is fully loaded
window.onload = init;
-->
</script>
</head>
<body>
<iframe id="reloader" width="500px" height="400px" src="http://www.google.com/"/>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum