使用 onload javascript 时出现问题
我已经像这样使用了Javascript onlaod:
function check()
{
var pic = new Image();
pic.src= "images/first.jpg";
pic.onload = function()
{
alert("Uploaded");
}
}
这是调用该函数的html代码。
<input type="button" onclick="check()" value="Check" />
它适用于 Safari 和 Firefox。但是对于 IE,第一次它可以工作,但是当我下次单击检查按钮时它不起作用。当缓存被清除时它也起作用。
谁能帮我看看这里可能会出现什么问题。
提前致谢
I have used Javascript onlaod like this:
function check()
{
var pic = new Image();
pic.src= "images/first.jpg";
pic.onload = function()
{
alert("Uploaded");
}
}
This is html code where the function is called.
<input type="button" onclick="check()" value="Check" />
It works for both safari and firefox. But with IE, first time it works but when I click on check Button next time it does not work. It also works when cache is cleared.
Can anyone help me what problem might occur here.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在 IE8 中应该不是问题。
众所周知,IE6(不确定 7)非常渴望使用缓存文件,并且从缓存中获取时,负载计算不正确(我记得有一个关于此的有趣的错误报告,请在 MS 的网站上查找)。
可以通过添加一个强制重新加载缓存文件的[无用]参数来解决:
pic.src= "images/first.jpg?nocache="+Math.random()
This should not be a problem in IE8.
IE6 (not sure about 7) is notoriously eager to use cached files, and when taking from the cache the load is not correctly calculated (I recall there being an interesting bug report on this, look for it on MS's site).
It can be solved by adding a [useless] parameter that forces a reload of the cached file:
pic.src= "images/first.jpg?nocache="+Math.random()
也许 onload() 太早了?
函数
jquery 使用一个在页面加载完成时执行的 。
也许您需要一些类似的功能。
perhaps the onload() is too early?
jquery uses a function
that is executed when the page has finished loading.
Perhaps you need some similar function.