Fancybox: Closing IFrame FancyBox loaded from another domain

Posted on 2012-08-21
Last Modified: 2013-01-13
I am able to successfully close an IFrame fancybox on the same domain using a hyperlink in the IFrame document, but not when it is called from another domain.  This appears related to security settings, at least in IE 9.
  + FancyBox
     IFrame >
     Click: parent.$.fn.fancybox.close();

SCRIPT5: Access is denied.

Is there a way to trust the remote domain so that I am able to add close buttons to augment than the standard circle X in the top right corner?
Question by:Officedoxs
    LVL 42

    Expert Comment

    by:David S.
    No. The only way to do that would be to use a proxy script, run under your domain.
    LVL 9

    Accepted Solution

    Your one option is using the change in fragment -hash- of main page url and make main page react to that change as you programmed before (closing fancybox in this case)

    something like ;
    // in iframe ;

    Open in new window

    and in main page you need to give ability to watch hashchanges
    add these
    if ("onhashchange" in window) { // event supported?
        window.onhashchange = function () {
    else { // event not supported:
        var storedHash = window.location.hash;
        window.setInterval(function () {
            if (window.location.hash != storedHash) {
                storedHash = window.location.hash;
        }, 100);
    function hashChanged(storedHash){
    if(storedHash =="close_fancybox") $.fancybox.close()

    Open in new window

    look at this fiddle

    you will just remove fancybox opener and change
     <a onclick="location+='#close_fancybox';return false;"
     <a onclick="parent.location+='#close_fancybox';return false;"

    Author Comment

    Thank you for your time and efforts here.

    I could be wrong, but this looks like it only works for an inline.  I am accessing an IFrame for a remote domain which Kravimir illudes to has the underlying cross-domain security wrinkles.

    I'm trying to overlay dynamic content from Site 2 on top of Site 1 with minor interaction between each other, such as closing the window.  I think I'm locked out on that account.
    LVL 9

    Expert Comment

    No this works with both inline and iframe. I cannot show you iframed version because i don't have chance  to change your files. If you cannot figure this out send both main and iframe html sources .Then i can show you the changes you need to do.

    Author Closing Comment

    Apologies for the delay, works great!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
    Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now