使用 onload javascript 时出现问题

发布于 2024-08-03 23:31:10 字数 434 浏览 2 评论 0原文

我已经像这样使用了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 技术交流群。

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

发布评论

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

评论(2

你的背包 2024-08-10 23:31:10

这在 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()

栀子花开つ 2024-08-10 23:31:10

也许 onload() 太早了?

函数

$(document).ready(function(){} 

jquery 使用一个在页面加载完成时执行的 。
也许您需要一些类似的功能。

perhaps the onload() is too early?

jquery uses a function

$(document).ready(function(){} 

that is executed when the page has finished loading.
Perhaps you need some similar function.

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