Javascript 在闭包/匿名函数中重新加载页面?

发布于 2024-09-13 01:55:29 字数 1333 浏览 6 评论 0原文

我正在使用 jQuery 为一些 DOM 元素设置动画,但我希望页面在动画完成时重新加载,所以我尝试了以下方法:

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);
                });
        });
    });
}

但是,当动画完成时,而不是重新加载页面,我收到以下错误:

未捕获的异常:[异常...“组件返回失败代码:0x80004005(NS_ERROR_FAILURE)[nsIDOMLocation.reload]”nsresult:“0x80004005(NS_ERROR_FAILURE)”位置:“JS框架::等...]

这是怎么回事?可以分页重新加载不能放在 function(){} 闭包中吗?

注意:浏览器:firefox 3.6.8。

受众浏览器:firefox 3+、chrome、safari 5+

注意:我也尝试过 window.location.reload(true) ; 返回了相同的错误,我尝试了 window.location.href=window.location.href 返回:

未捕获的异常:[异常... “组件返回失败代码: 0x80004003(NS_ERROR_INVALID_POINTER) [nsIDOMLocation.href]” ns结果: “0x80004003 (NS_ERROR_INVALID_POINTER)”位置: “JS框架::等...]

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...]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撑一把青伞 2024-09-20 01:55:29

window.location.reload() 不接受任何参数,与调用 JavaScript 函数不同,在调用浏览器 API 函数时,浏览器对可以/必须传递的参数数量非常严格到函数。

编辑:感谢OP指出这一点,Firefox至少允许将一个参数传递给reload()。 (请参阅http://developer.mozilla.org/en/window.location

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)

北座城市 2024-09-20 01:55:29

问题解决了。我所做的是打开一个 iframe,然后将其删除。我想在 iframe 删除时触发页面重新加载,但是,脚本是从 iframe 内运行的,因此当 iframe 不再存在时,脚本不再有可以运行的上下文。本质上,当窗口甚至不存在时,我无法调用 window.location !然而,window.parent.location 也不起作用,因为一旦 iframe 消失,我实际上是在调用 null.parent,它也不存在。

哈哈哈,我想我可能需要稍微改变一下设计。谢谢你们!

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!

煮茶煮酒煮时光 2024-09-20 01:55:29

我有同样的问题。
我通过仅在加载页面时调用代码解决了这个问题。
对我来说,以下两个脚本都有效。

window.addEventListener ("DOMContentLoaded", doStuff, false);

如果

window.addEventListener ("load", doStuff, false);

两者都适合您,则更喜欢第一个。

I had the same exact problem.
I solved this by calling the code only when the page was loaded.
For me both the below scripts worked.

window.addEventListener ("DOMContentLoaded", doStuff, false);

and

window.addEventListener ("load", doStuff, false);

If both works for you prefer the first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文