我编写了一个小的 javascript
代码来获取用户浏览器的屏幕尺寸。首先,img
元素的 src
属性设置为静态图像 url
。当页面完全加载时,以下代码计算屏幕尺寸并将其发送到服务器,方法是将现在包含尺寸的 img
的 src
更改为 uri
参数(稍后在服务器端,这些参数将被处理):
<img id="screenres" src="http://static.example.com/image.jpg" alt="" width="20" height="20" />
<script>
window.onload=function(){
var w =Math.round(screen.width);
var h =Math.round(screen.height);
document.getElementById("screenres").src=
"http://www.example.com/screen/"+w+"/"+h;
}
</script>
这种方法在桌面浏览器中完美运行,但对于移动用户来说却失败了。您能告诉我这种方法有什么问题吗?预先感谢您提出解决此问题的建议。
最佳答案
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
关于javascript - 通过javascript更改图片的src,在移动设备上不生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29905139/