4

I want, if possible, to open a fancybox as a child of a parent using iframe.

Is it possible to open an iframe ( constructed with fancybox ) and make the browser belive I have opened it using window.open so that functions like window.close() or window.opener.location.reload() still work ?

The fancybox code is simple :

$("#custom_alert").fancybox({
        'width'             : w,
        'height'            : h,
        'autoScale'         : false,
        'transitionIn'      : 'none',
        'transitionOut'     : 'none',
        'type'              : 'iframe',
        //'scrolling'         : 'no'
    }).trigger('click');

EDIT : I cannot use functions like :

function closePopup(){
     window.close();
     if(parent.jQuery().fancybox) {
      parent.jQuery.fancybox.close();
     }
}

I MUST use the window.close() function.

A fix for my problem can be also to tell me why I cannot rewrite the close function like I did with alert or confirm.

EDIT 2: Thanks Raohmaru and JFK, after seeing the fiddle worked, I have made the function I need, but a strange thing ( for me ) is happening :

The working code, if anybody else needs it is :

(function() {
  var proxied = window.close;
  window.close = function() {
    if(parent.jQuery().fancybox) {
        parent.jQuery.fancybox.close();
    }
    else
        return proxied.apply(this, arguments);
  };
})();

This works for both fancybox and window.open cases.

My problem was this : I include some .js files in index :

overwrites
fancybox pack
general scripts

If I put that function in general scripts, it works perfectly, but if I put it in the overwrites js ( where it should be ) it doesn't work ( no console errors, just nothing happens ). I have tried swapping positions on the includes, but no luck.

This is why I couldn't make it work from the first place, because I always included it in the overwrites .js.

Cosmin
  • 1,482
  • 12
  • 26
  • Which browsers you cannot override the close function? You can do it in Firefox (tested in 13.0.1), Chrome (21.0), even in Internet Explorer 8 and below. But you cannot in IE9 in standard mode ([link](http://social.msdn.microsoft.com/Forums/en-HK/iewebdevelopment/thread/eb49366b-bd44-4341-be36-fb75a6dab40e)). – Raohmaru Sep 13 '12 at 09:13
  • The close override function did not work for me on any browser, and JS didn't throw any errors. – Cosmin Sep 13 '12 at 09:41
  • This example doesn't work for you? http://jsfiddle.net/ufPbf/. Also note that you cannot rewrite an object of the parent window within an iframe. – Raohmaru Sep 13 '12 at 11:55
  • I guess because this `parent.jQuery.fancybox.close();` doesn't make sense if fancybox script hasn't been loaded. I think `overwrites` should be the last script to be included. – JFK Sep 13 '12 at 17:14

1 Answers1

1

What you can do is to validate IF the page has been opened in fancybox and perform the proper fancybox methods, otherwise just use your regular javascript methods (i.e. window.close())

Having said that, within an opened page (either via window.open() or fancybox) you may have a function to close it like

function closePopup(){
 window.close();
}

associated to a close button/link like

<a href="javascript:;" onclick="closePopup()" >Close Window</a>

that for sure will close the popup window but not the fancybox so you can add the following :

function closePopup(){
 window.close();
 if(parent.jQuery().fancybox) {
  parent.jQuery.fancybox.close();
 }
}

and that will work for either the popup window or fancybox.

JFK
  • 40,963
  • 31
  • 133
  • 306
  • Thank you for this, but it does not help me. I have overwritten some default JS functions and I have already tried to overwrite the close function, but unfortunately I cannot rewrite this particular one, don't know why. I must not add new functions, I only have to use the existing ones, so adding a `closePopup()` function does not help. – Cosmin Sep 03 '12 at 07:32
  • is just an example of how to do it, you can edit your existing functions (and add the conditional statement to see if fancybox is opened) to achieve the same result – JFK Sep 03 '12 at 16:23
  • Please read my top comment. I touth I was clear enough. I cannot rewrite the close function ( the JS default one ) like I have overwriten other functions ( alert, confirm, etc... ) and I cannot add new functions also. I MUST use `window.close()` and the other JS functions and cannot us a function like `function closePopup(){ window.close(); }` – Cosmin Sep 06 '12 at 10:33
  • @Cosmin you never said how you did "rewrite" your other js functions in your code. – JFK Sep 06 '12 at 22:36
  • It is irrelevant for the question, for more info you can check http://stackoverflow.com/questions/1729501/javascript-overriding-alert . EX : `window.alert = function() { console.log(arguments); };` - to supress `alert` and use `console.log` instead. But this does not work for `window.close` for some reason. – Cosmin Sep 07 '12 at 06:12