日期:2014-05-16  浏览次数:20307 次

js简单的应用,请大家指点
<img   src= '. '     alt= " "   onload= "if(this.width> 135){this.resized=true;   this.width=135;}   else   {   if(this.height> 108){this.resized=true;   this.height=108;}   } "   border= "0 ">
这段代码,发现刚打开新页面的时候会执行,但有时候点后退,或刷新,图片的大小会按原图显示,ONload里面没有执行。估计是浏览器缓存了。请问:一般怎么解决?

------解决方案--------------------
学习
------解决方案--------------------
在window.onload事件处理就可以了。
------解决方案--------------------
做个刷新的东西试试
------解决方案--------------------
肯定和其他代码有冲突
------解决方案--------------------
try
-----------------------

<div id= "photo ">
<img src= "aaa.gif ">
<img src= "aaa.gif ">
<img src= "aaa.gif ">
</div>

<script language= "javascript ">
document.body.onload = function(){
var photos = document.getElementById( "photo ").getElementsByTagName( "img ");
for(var i=0;i <photos.length;i++){
if(photos[i].width> 135){
photos[i].resized=true;
photos[i].width=135;
}
if(photos[i].height> 108){
photos[i].resized=true;
photos[i].height=108;
}
}
}
</script>
------解决方案--------------------
不让缓存的话,方法还不是一般的多:

方法一
<%
response.setHeader( "Cache-Control ", "no-cache "); //HTTP 1.1
//or response.setHeader( "Cache-Control ", "no-store ");//HTTP 1.1
response.setHeader( "Pragma ", "no-cache "); //HTTP 1.0
response.setDateHeader ( "Expires ", 0); //prevents caching at the proxy server
%>

方法二
import java.text.SimpleDateFormat;
import java.util.Date;

SimpleDateFormat dateFormat = new SimpleDateFormat( "EEE, dd MMM yyyy - HH:mm:ss z ");
response.setHeader( "Expires ", dateFormat.format(new Date()));

方法三
<meta http-equiv= 'Expires ' content= '-10 '>
<meta http-equiv= 'Pragma ' content= 'No-cache '>
<meta http-equiv= 'Cache-Control ', 'private '>

------解决方案--------------------
其实原理都是设置页面header
方法三最省事
------解决方案--------------------
可能是你的代码有问题
<img onload= 'if(this.width> 560){this.height=(this.height/this.width)*560;this.width=560; '}>
------解决方案--------------------
奇怪的因为,还是跟缓存有关