How to determine screen resolution in javascript

I want javascript to detect the screen resolution myself, because I may have a different LCD, and each one has a different size. this is the temporary code i have.

<html>
<head>
<title>ViewImage</title>
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
//   -->
</script>
</head>
<body onload="JavaScript:timedRefresh(1);">

<img src = "screenshot.jpeg" width="1014" height="751">
</body>
</html>

I tried putting javascript in code

<img src = "screenshot.jpeg" <script type="text/JavaScript">"width ="screen.width </script>>

but it failed. when I open this, I wish he could get permission on his own, can anyone help me with this problem? thanks in advance

+3
source share
5 answers

In your case, this would be enough:

<img src = "screenshot.jpeg" width ="100%" />

jsFiddle Demo

, , , body , . , img , ( body).


. Javascript , . <script> - HTML-, ( , Javascript) . HTML Javascript (, PHP, Python .. ). DOM, .

+2

, , :

JavaScript: onLoad,

<a href="javascript: doSomething()" />Link</a>

1 ( , 1000).

, eval:

function timedRefresh(timeoutPeriod) {
    setTimeout(function() { 
        location.reload(true); 
    }, timeoutPeriod);
}
+2

You can try this

window.screen.availHeight for getting height
window.screen.availWidth  for getting width

Thank.

+1
source

The CSS3 properties 'size' and 'cover' also help in this situation only if you are not very concerned about older versions of IE. And no need to use JavaScript

Demo - http://jsfiddle.net/fHwu8/

body{
    background: #000 url(http://www.psdgraphics.com/wp-content/uploads/2009/02/abstract-background.jpg) left top fixed no-repeat;
    background-size: 100% 100%;
    -moz-background-size: 100% 100%; /*cover*/
    -webkit-background-size: 100% 100%;
    -o-background-size: 100% 100%;
}
+1
source

to try:

window.screen.availHeight
window.screen.availWidth
0
source

All Articles