1

I have an iframe on my page where you can checkout my latest web development pages, and so on. Is it possible to refresh it with regular JavaScript or jQuery?

<iframe src="projektit" style="width:100%; height:400px; border:none;" id="iframe"></iframe>

I'm thinking about something like this.

$.ajax({
    url: "../",
    success: function (data) {
        $("#iframe").html(data);
    }
});

Any ideas?

fabian
  • 80,457
  • 12
  • 86
  • 114
  • 1
    Another option would be to make the page within the iframe refresh itself, either using a `` tag or a simple `window.setTimeout` setup... but can understand if you want the iframe to be refreshed externally. **Edit** unless I've misunderstood what you are trying to do – freefaller Aug 25 '12 at 16:57
  • Not an option. The iframe must be only refreshed if the user wants. –  Aug 25 '12 at 17:00
  • Fair enough, that would make sense. In which case Gabe provides a good answer – freefaller Aug 25 '12 at 17:03
  • possible duplicate of [What's the best way to reload an iframe using JavaScript?](http://stackoverflow.com/questions/86428/whats-the-best-way-to-reload-an-iframe-using-javascript) –  Oct 27 '14 at 15:18

2 Answers2

1
<iframe id="homepage" frameborder="1" width="400" height="300" src="http://jbcurtin.com/"></frame>
<script>
  var frame=$("#homepage")
  frame.attr('src',frame.attr('src'))
  // None jquery:
  var frame=document.getElementById('homepage')
  frame.src=frame.src;
</script>

http://jsfiddle.net/ekpAm/

jbcurtin
  • 1,793
  • 2
  • 14
  • 23
0

jsFiddle

To refresh an iframe I would suggest just changing the src value. Changing the src value will trigger the iframe to refresh.

$('#iframe').prop('src', url);

If you notice it's not refreshing, you could always attach a bogus query string value to the end of the url in the case of it being cached.

var q = new Date(); // use current date obj as query string value
$('#iframe').prop('src', url + '?q=' + q.getTime());
Gabe
  • 49,577
  • 28
  • 142
  • 181
  • I'm unable to get this working, I'll make a fiddle about this. –  Aug 25 '12 at 17:04
  • @ChristianNikkanen Google is not allowing you to use them as an iframe source. Here is a working example: http://jsfiddle.net/XG3Ea/ – Gabe Aug 25 '12 at 17:11
  • The code seems to be working... But not on my server... As I have managed to remove jquery from my page. But nope, it's not working. –  Aug 25 '12 at 17:20
  • @ChristianNikkanen Perhaps you could use fiddler to see what requests are being made – Gabe Aug 25 '12 at 17:39
  • It's probably something wrong in server configuration or something. –  Aug 26 '12 at 07:32