Javascript 在闭包/匿名函数中重新加载页面?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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)
问题解决了。我所做的是打开一个 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!
我有同样的问题。
我通过仅在加载页面时调用代码解决了这个问题。
对我来说,以下两个脚本都有效。
如果
两者都适合您,则更喜欢第一个。
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.
and
If both works for you prefer the first.