4

This issue is very similar to jQuery: Fancybox produces a loop of errors in chrome using ajax, although in the other issue ajax is being used. I'm using inline to present a div.

I can open the fancybox containing the div once, and close it again. But if I open it again, I see the following error in the console, and the page changes to a random portion of text from the page from a completely different section:

Uncaught TypeError: Cannot call method 'width' of undefined

I have Fancybox set up on this page with the following:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="./includes/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<script type="text/javascript" src="./includes/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#admin_link").fancybox({
            'titlePosition'     : 'inside',
            'transitionIn'      : 'slide',
            'transitionOut'     : 'fade',
            'type'              : 'inline'
        });
    });
</script>
<link rel="stylesheet" type="text/css" href="./includes/fancybox/jquery.fancybox-1.3.4.css" media="screen" />

The div is:

<div style="display: none;">
                <div id="admin_why_text" style="width:400px;height:300px;overflow:auto;">
                    Some text about why this is needed.
                </div>
            </div>

And this div is opened via this link:

      <ul><li>example point <br />=> <a id="admin_link" href="#admin_why_text" title="Why is.....?">why is...?</a></li></ul>

As you can see, based on the previous issue that the other user saw with ajax (not my issue, see link above), I have tried defining the type manually in my code. This unfortunately made no difference.

Would anybody have any other suggestions? Or has anyone seen this before when using the inline type?

Edit: Minor addition, other types are working fine. I'm using the iframe type to display youtube videos, and they can be closed and reopen without any issues.

Edit2: I've found that when the inline box loads, my div is replaced with the following:

<div style="display: none;">
    <div class="fancybox-inline-tmp" style="display: none;"></div>
</div>

When the box is closed, my div isn't put back. I need to find where this has gone, I can probably use the onCleanup callback to set it back.

Community
  • 1
  • 1
Tiago
  • 1,984
  • 1
  • 21
  • 43
  • 1
    you only need a single instance of jQuery so either this http://code.jquery.com/jquery-latest.js or this http://code.jquery.com/jquery-latest.min.js but not both – JFK Dec 31 '13 at 05:33
  • Thanks, I wasn't sure on that. I've taken out the min instance, but the issue persists. Would you have any other ideas? – Tiago Dec 31 '13 at 09:45
  • On more investigation, my div seems to be replaced with this when the box loads, but isn't set back when the box closes:
    – Tiago Dec 31 '13 at 11:55
  • possible duplicate of [Fancybox doesn't work with jQuery v1.9.0 \[ f.browser is undefined / Cannot read property 'msie' \]](http://stackoverflow.com/questions/14344289/fancybox-doesnt-work-with-jquery-v1-9-0-f-browser-is-undefined-cannot-read) – JFK Dec 31 '13 at 20:38
  • I didn't notice but you are using fancybox v1.3.4 and that version doesn't work with jQuery 1.9+ see http://stackoverflow.com/a/14344290/1055987 for a workaround – JFK Dec 31 '13 at 20:39
  • Why the down-vote? I already applied the linked workaround before I saw this issue. The not working events were the cause of the issue, I added this to the answer here. – Tiago Jan 02 '14 at 09:02

4 Answers4

9

I've found the answer to this problem. I'm using the latest jQuery, in which global events are deprecated. There are two key issues I have found in using the v1 FancyBox with the latest jQuery:

  1. The issue described in https://github.com/fancyapps/fancyBox/issues/485
  2. This issue.

Further to the edits suggested from issue 1, the following changes fixed this for me:


In the affected page, find:

<script type="text/javascript" src="./includes/fancybox/jquery.fancybox-1.3.4.pack.js"></script>

Change to the following to use the non packed version:

<script type="text/javascript" src="./includes/fancybox/jquery.fancybox-1.3.4.js"></script>

In the "jquery.fancybox-1.3.4.js" file, make the following changes.

Find: $.event.trigger('fancybox-cancel');

Replace with: $('.fancybox-inline-tmp').trigger('fancybox-cancel');

Find: $.event.trigger('fancybox-change');

Replace with: $('.fancybox-inline-tmp').trigger('fancybox-change');

Find: $.event.trigger('fancybox-cancel');

Replace with: $('.fancybox-inline-tmp').trigger('fancybox-cancel');

Find: $.event.trigger('fancybox-cleanup');

Replace with: $('.fancybox-inline-tmp, select:not(#fancybox-tmp select)').trigger('fancybox-cleanup');

Hopefully this will help others who get stuck on the same issue.

Tiago
  • 1,984
  • 1
  • 21
  • 43
2

The following may work for you without needing to hack the fancybox.js file.

$(document).ready(function() {
    $("#admin_link").on("click", function (e) {
        $.fancybox($(#admin_why_text).text(), {
            'title'             : $(this).attr('title'),
            'titlePosition'     : 'inside',
            'transitionIn'      : 'slide',
            'transitionOut'     : 'fade',
            'type'              : 'inline'
        });
    });
});

This effectivly changes your code to operate with the 'ajax method' rather than the 'inline method' but still sources your information from the same place & will not replace your id="admin_why_text" element with the class="fancybox-inline-tmp" element.

A more generic version, which will target the text element based on the href of the trigger element would work as follows :

$(document).ready(function() {
    $("#admin_link").on("click", function (e) {
        $.fancybox($("#" + $(this).attr('href').substring(1)).text(), {
            'title' : $(this).attr('title'),
            'titlePosition'     : 'inside',
            'transitionIn'      : 'slide',
            'transitionOut'     : 'fade',
            'type'              : 'inline'
        });
    });
});
Joe Sinfield
  • 546
  • 4
  • 8
  • The proposed solution does not work with jquery 1.11 and fancybox 1.3.4, fancybox tries to open a URL instead of displaying the inline HTML. – basZero Aug 21 '15 at 11:15
0

Just use one Jquery Library file.you can use either

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>

or

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

but not both!

Praveen Kumar
  • 206
  • 4
  • 14
  • 1
    Thanks for the answer. Unfortunately this wasn't the problem, although I've corrected the above. I found the issue and fixed it, global events are deprecated in the later jQuery, breaking some of the fancybox functionality (see my answer to this question). – Tiago Dec 31 '13 at 12:41
  • the -1 is because that was already suggested in the comments area 7 hours before your answer. PS. Jquery should be jQuery – JFK Dec 31 '13 at 20:41
  • Good point, sometimes the big fails cames with one or multiple one-liners fails – webo80 Jul 08 '15 at 11:27
0

I have added following into function that loads fancybox,

var txt1 = "<div id='inline1' style='width:400px;overflow:auto;font-size:12px;'></div>";

                if ($("#inline1").length) {
                }
                else {
                    $("#ix01").append(txt1);
                }

so if div inline1 not exists add it to parent div "ix01" , works fine for me.