javascript - 通过javascript更改图片的src,在移动设备上不生效

标签 javascript html mobile-browser

我编写了一个小的 javascript 代码来获取用户浏览器的屏幕尺寸。首先,img 元素的 src 属性设置为静态图像 url。当页面完全加载时,以下代码计算屏幕尺寸并将其发送到服务器,方法是将现在包含尺寸的 imgsrc 更改为 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/

相关文章:

html - 如何使用填充将文本字段拉伸(stretch)到父 div 的宽度?

javascript - forEach() 和 reduce() 不适用于参数

javascript - 在 span 和其他内联标签上启用 CKEditor4 内联

javascript - 通过正则表达式匹配拆分字符串

javascript - jQuery clone() 附加到多个元素而不是仅一个元素

android - 从移动浏览器打开原生 Spotify 应用程序

javascript - 如何禁用(防止)移动网站水平滑动?

html - 在 Sass 中,我们如何根据宽度值分配高度?

JQuery 点击事件不适用于移动浏览器中的 iframe

HTML/CSS : table font size different in mobile device