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
82.9k12 gold badges99 silver badges124 bronze badges
asked Aug 25, 2012 at 16:52
4
  • 1
    Another option would be to make the page within the iframe refresh itself, either using a <meta> 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 Commented Aug 25, 2012 at 16:57
  • Not an option. The iframe must be only refreshed if the user wants. Commented Aug 25, 2012 at 17:00
  • Fair enough, that would make sense. In which case Gabe provides a good answer Commented Aug 25, 2012 at 17:03
  • possible duplicate of What's the best way to reload an iframe using JavaScript? Commented Oct 27, 2014 at 15:18

2 Answers 2

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/

answered Aug 25, 2012 at 17:32
Sign up to request clarification or add additional context in comments.

2 Comments

This isn't actually doing anything.
This worked for me! frame.src=frame.src; bind the latest url to the iframe
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());
answered Aug 25, 2012 at 16:53

5 Comments

I'm unable to get this working, I'll make a fiddle about this.
@ChristianNikkanen Google is not allowing you to use them as an iframe source. Here is a working example: jsfiddle.net/XG3Ea
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.
@ChristianNikkanen Perhaps you could use fiddler to see what requests are being made
It's probably something wrong in server configuration or something.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.