Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Fancybox can only show inline window once, second attempt fails with error: "Uncaught TypeError: Cannot call method 'width' of undefined"

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.

like image 719
Tiago Avatar asked Dec 20 '22 19:12

Tiago


1 Answers

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.

like image 171
Tiago Avatar answered Dec 22 '22 10:12

Tiago