Reload IFrame and manage Same-Origin policies with JQuery

jquery-logoFor a project, I had to dynamicaly reload an IFrame (within a JQuery dialog box) and include cross domain pages.
To load the IFrame and set the source :

$(FrameID).attr("src", URL);

To reload the IFrame you can use :


But if the content come from another domain you will be denied access to the iframe’s contentDocument property due to the same-origin policy.
To force the cross-domain iframe to reload if your code is running on the iframe’s parent page, you can set it’s src attribute to itself.

var iframe = document.getElementById(FrameID);
iframe.src = iframe.src;

But be carefull, this is a hack ! So if you do something like this in production code, comment it, or the next one would probably remove this code who seems totally useless.

Published by Emmanuel Istace

Musician, Software developer and guitar instructor.

2 thoughts on “Reload IFrame and manage Same-Origin policies with JQuery

    1. Yeah, sorry, might be outdated 7 years later, was really a hackish way to get around an hackish solution, if possible, I wouldn’t recommend at all using iframe (even for 2013, don’t remember why, but was probably integration with some stuff I don’t have hands on) and if you have CORS issues, might consider taking a look at your server config/http header (or if not possible, look for a “cors proxy”, still hackish, but perhap’s “less messy”) πŸ™‚
      Hope you’ll find a (clean) solution ! πŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: