SoFunction
Updated on 2025-03-01

JS determines whether the device is a PC and adjusts the image size


<html>
<head>
<script type="text/javascript">
/* Determine whether the device is a PC */
function isPC() {
var userAgentInfo = ;
var Agents = new Array("Android", "iPhone", "SymbianOS","Windows Phone", "iPad", "iPod");
var flag = true;
for ( var v = 0; v < ; v++) {
if ((Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}

/* Resize the image */
function AutoResizeImage(maxWidth, maxHeight, objImg) {
var img = new Image();
= ;
var hRatio;
var wRatio;
var Ratio = 1;
var w = ;
var h = ;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth == 0 && maxHeight == 0) {
Ratio = 1;
} else if (maxWidth == 0) { //
if (hRatio < 1) Ratio = hRatio;
} else if (maxHeight == 0) {
if (wRatio < 1) Ratio = wRatio;
} else if (wRatio < 1 || hRatio < 1) {
Ratio = (wRatio <= hRatio ? wRatio : hRatio);
}
if (Ratio < 1) {
w = w * Ratio;
h = h * Ratio;
}
= h;
= w;
}

/* Set the scaling policy for different devices */
function setImg(tagid,pcWidth,pcHeight,appWidth,appHeight){
var tag=(tagid);
var images=("img");
for(var i=0;i<;i++){
if(isPC){
AutoResizeImage(pcWidth, pcHeight, images[i]);
}else{
AutoResizeImage(appWidth, appHeight, images[i]);
}
}
}
=function(){
setImg('imgDIV',300,0,300,0);
}
</script>
</head>
<body>
<div >
<img alt="" src="http://192.168.1.116:9999/ffzx/news/20140205/015212022_1.jpg" />
<div>
<img alt="" src="http://192.168.1.116:9999/ffzx/news/20140208/" />
</div>
</div>
<br>
</body>
</html>