Ormoz
Ormoz

Reputation: 3013

Changing src of Image by javascript, does not take effect in mobile devices

I have written a small javascript code to get screen dimensions of the user browser. At first, the src attribute of an img element is set to a static image url. When the page is fully loaded, the following code, calculate screen dimensions and send them to the server by changing the src of the img that now contains the dimensions as uri parameters (Later, at server side, these parameters will be processed):

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

This approach works perfectly in desktop browsers, but for mobile users it fails. Could you please tell me what is wrong in this approach? Thank you beforehand for your suggestions to overcome this problem.

Upvotes: 0

Views: 315

Answers (1)

Ed Knowles
Ed Knowles

Reputation: 1925

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

Upvotes: 2

Related Questions