views:

153

answers:

2

I am using jQuery to animate some DOM elements, but I want the page to reload when the animations are complete, so I tried this:

function childDisablePopup()
{
    $("#popup2",window.parent.document).animate({
        width: "0px",
        marginLeft: 0
    }, "fast",
    function()
    {
        $("#popup2",window.parent.document).animate({
            height: "0px",
            marginTop: 0
        }, "fast",
        function()
        {
            $("#backgroundPopup",window.parent.document).fadeOut("fast",
                function()
                {
                    $("#popup2",window.parent.document).remove();
                    window.location.reload(true);
                });
        });
    });
}

However, when the animation completes, instead of the page reloading, I get this error:

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMLocation.reload]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: etc...]

What's going on? Can page reloads not be put in a function(){} closure?

Note: Browser: firefox 3.6.8.

Audience browsers: firefox 3+, chrome, safari 5+

Note: I also tried window.location.reload(true); which returned the same error, and I tried window.location.href=window.location.href which returned:

uncaught exception: [Exception... "Component returned failure code: 0x80004003 (NS_ERROR_INVALID_POINTER) [nsIDOMLocation.href]" nsresult: "0x80004003 (NS_ERROR_INVALID_POINTER)" location: "JS frame :: etc...]

+2  A: 

window.location.reload() doesn't take any arguments, and unlike calling a JavaScript function, when calling a browser API function the browser is very strict about the number of arguments that can/must be passed to the function.

EDIT: Thanks to OP for pointing this out, Firefox at least allows one parameter to be passed to reload(). (see http://developer.mozilla.org/en/window.location)

Ryan Tenney
Actually, according to the MDC: https://developer.mozilla.org/en/window.locationreload() takes 1 parameter, forceget, if set to true, it forces a request to the server instead of simply reloading from browser cache.
Razor Storm
Good call, my goto JS reference has failed me. http://www.w3schools.com/jsref/met_loc_reload.asp
Ryan Tenney
Hmm, in that case the forceget parameter might be a firefox only implementation. the window.location object is not part of ECMA's javascript specifications, so I guess browser dependent implementations is likely. I can't seem to find webkit's javascript documentation however.
Razor Storm
A: 

Problem solved. What I was doing is opening up an iframe, and then removing it. I wanted to trigger the page reload on iframe removal, however, the script is being run from WITHIN the iframe, so when the iframe no longer exists, the script no longer has a context to run from. In essence, I can't call window.location when window doesn't even exist! However, window.parent.location also doesn't work since once the iframe disappears, I'm effectively calling null.parent, which doesn't exist either.

Hahaha, I guess I might have to change up the design a bit. Thanks guys!

Razor Storm