1

I'm using jQuery to show / hide a div which happens to contain an iframe. It works great with just standard 'show' and 'hide' methods.

So now I want to get a little fancy and add some effects from jQuery UI (http://jqueryui.com/effect/) but suddenly my iframes are getting reloaded every time I show / hide them.

Here is a fiddle to demonstrate: http://jsfiddle.net/BnZzk/1/ And here is the code since SO is forcing me to add it:

<style>
div {
    height: 200px
}
span {
    display: block;
    padding-bottom: 10px;
    margin-bottom: 10px;
    border-bottom: 1px solid black;
}
</style>

<div>
    <iframe src="http://www.wikipedia.org/"></iframe>
</div>
<input type="button" value="Go"/>
<hr/>
<div id="msgs"></div>

<script>
$(function () {

var container = $('div'),
    ifrm = $('iframe'),
    msgs = $('#msgs')
    delay = 10; //change me to adjust delay in seconds between actions

$('input').on('click', normal);
log('First let the iframe load and then clear your network console -- click the "Go" button to get started.');

function log (msg) {
    msgs.append('<span>' + msg + '</span>');
}

function normal () {

    ifrm.
        hide(400, function () {

            log('That was a standard hide with no effect -- is your network console still empty? OK we\'ll pause for ' + delay + ' seconds and then do a standard show.');

            ifrm.
                delay(delay * 1000).
                show(400, function () {
                    log('That was a show with no effect -- is you network console *still* empty? Great! Let\'s try some effects.');
                    log('------------------------<br/>' +
                    '-- Begin Effects --<br/>' +
                    '------------------------<br/>');
                    withEffect();
                });

        }); //hide

} //normal

function withEffect () {

    log('We\'ll pause for another ' + delay + ' seconds -- get ready to watch your network console.');

    ifrm.
        delay(delay * 1000).
        hide('fold', {mode:'hide'}, 400, function () {

            log('That was a hide with effect -- is your network console flooded? Mine too :-( We\'ll wait ' + delay + ' seconds while you clear your console again.');

            ifrm.
                delay(delay * 1000).
                show('fold', {mode:'show'}, 400, function () {
                    log('That was a show with effect -- is your network console flooded again? Bummer ...');
                });

        }); //hide

} //withEffect

});
</<script>

Any idea how I can keep the fancy effects but not refresh the content of my iframes?

2
  • Did you ever come up with a solution for this that you would be able to share? I believe this is the same issue I am running into at the moment. Commented Mar 26, 2014 at 23:24
  • Yes, looks like I never accepted the right answer though. I just switched to using animate directly to avoid re-appending the iframe. Commented Apr 16, 2014 at 16:28

2 Answers 2

3

It's happening because this effect reorganizes the DOM, putting a DIV wrapper around the IFRAME, so when the IFRAME is "reappended" the reload happens! You can see this behavior using the Google Chrome elements inspector.

To solve I suggest you apply the effect in a parent DIV from your IFRAME but not using the effect plugin. Check out the http://api.jquery.com/animate/, manipulating the width and height style properties.

Sign up to request clarification or add additional context in comments.

2 Comments

I see. Well it looks like the effects are already using 'animate' internally. I just need to get rid of the wrapper. Thanks for pointing me in the right direction.
@Jamillo Santos, Could you please some sample code to prevent the div warpper from being put around the iframe? Probably, in the complete function of jquery ui toggle method, one needs to find that wrapper div and then remnove it using jquery.
2

As @Jamillo Santos's answer, 'reappend' issues of iFrame.

if you are using dialog widget or its extension of jQueryUI and want to prevent this situation, just redefine _moveToTop() function of your widget implementation as below.

_moveToTop: function() {
    return null;
},

1 Comment

I am using jquery ui core + effects download to animate using the jquery ui toggle method and passing the toggle method a jquery ui effect. Would you know what function I will need to re-define? I am not using the dialog widget or extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.