Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How to refresh an Iframe

  1. #1
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to refresh an Iframe

    I've been trying to figure this out for a couple days. It seems like it should be simple, but apparently, it's not. How do I refresh an Iframe with Javascript?

  2. #2
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    I don't really know, it might have a window object of it's own but I think this will work:

    var refreshIFrame = function (iframeID) {
    var src = document.getElementById(iframeID).src;
    document.getElementById(iframeID).src = null;
    document.getElementById(iframeID).src = src;
    }

  3. #3
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well, I've tried this:

    var a=document.getElementById("a");
    a.src=a.src;

    And that works, but only to a certain extent. I'll explain what I mean, so bear with me.

    Let's say I have an iframe like this: <iframe src="http://google.com" name="ifram" id=a height=780 width=1075></iframe>

    Now let's say that, for some reason, there's a link to yahoo.com on google.com. If I go to yahoo.com, and then try to do "a.src=a.src" then the iframe refreshes, but it goes back to google.com instead of refreshing yahoo.com.

  4. #4
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    Oh I see, well, I'll check on W3Schools and see if there's anything else I can find.

  5. #5
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    var refreshIFrame = function (iframeID) {
    var url = document.getElementById(iframeID).contentDocument.URL;
    document.getElementById(iframeID).contentDocument.URL = null;
    document.getElementById(iframeID).contentDocument.URL = url;
    }

    There! That should work. contentDocument is like document but for the iframe, and then theres the URL. Anything that might not work?
    Last edited by ???; 07-01-2007 at 03:33 PM.

  6. #6
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I looked at the firefox error console and it said this: "Error: uncaught exception: Permission denied to get property HTMLDocument.URL"

  7. #7
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    Well for me it just said it's a read only var. But that sucks.

  8. #8
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I tried this:

    "function refreshIFrame(iframeID) {
    var a=document.getElementById(iframeID);
    var url = a.contentWindow.URL;
    a.contentWindow.URL = null;
    a.contentWindow.URL = url;
    }"

    And in IE, it said "Permission Denied."

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Should be more simple than that.

    Code:
    <iframe name="test" src="a.htm" width="200" height="200"></iframe>
    <a href="#" onClick="frames.test.location='b.htm';">click</a>
    It works with or without the part in red, but I'm assuming that may not be the case for all browsers. Test it yourself.

    That's a working chunk of html for a page, but if you just want the script itself:
    framename.location='newpage.htm';

    However, since you're wanting it to refresh, use [frame].src to get the current value.

    Here's a function that should work:
    Code:
    function refreshframe(id) {
    frames.id.location=frames.id.location;
    }
    However, it doesn't seem to work for me. I've tried various combinations and it's not doing anything. Someone should know. I'm just guessing.
    This does work, though, with specifying a particular frame in the script (frames.myname.location for example), so it's something about passing the name to the id variable.


    Note that though you are specifying an iframe, this would work for any frame, so there is no need to specify the refresh script as for iframes.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Is it possible that it's not working because my page and the other page are on different domains?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •