2015-04-27 51 views
0

我已经编写了一个小的javascript代码来获取用户浏览器的屏幕尺寸。首先,将img元素的src属性设置为静态图像url。当页面满载时,通过更改imgsrc(现在包含尺寸为uri参数(稍后,在服务器端,这些参数将被处理)),将以下代码计算屏幕尺寸并将其发送到服务器:通过javascript更改图像的src,不会在移动设备中生效

<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> 

这种方法在桌面浏览器中完美工作,但对移动用户来说却失败了。你能告诉我这种方法有什么问题吗?预先感谢您提出解决此问题的建议。

+1

当你说它不起作用,它会失败还是不按预期显示?屏幕可能很小,但通常桌面环境中的屏幕很大。 –

+0

您是否有指向桌面的工作页面的链接,但不支持移动设备? –

+0

@Anthony,它不会将http请求发送到服务器。 – Ormoz

回答

2
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0) 
var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0) 
+2

解释会有帮助。 –