简体   繁体   中英

Can't show javascript variable in html

This is the code:

<div id="screen1">You do not have javascript enabled.</div>
<div id="screen2">You do not have javascript enabled.</div>
<script>
   document.getElementById("screen1").innerHTML = return window.screen.availHeight;
   document.getElementById("screen2").innerHTML = return window.screen.availWidth;
</script>

The output for both screen1 and screen2 is You do not have javascript enabled. , I thought this way would work because I used this before and it worked:

<div id="locale">You do not have javascript enabled.</div>
<script>document.getElementById("locale").innerHTML = getLang();</script>   

If you remove the return keyword, everything will work as normal. Since you're not defining/inside of a function, the return keyword is out of place.

 document.getElementById("screen1").innerHTML = window.screen.availHeight; document.getElementById("screen2").innerHTML = window.screen.availWidth; 
 <div id="screen1">You do not have javascript enabled.</div> <div id="screen2">You do not have javascript enabled.</div> 

 document.getElementById("screen1").innerHTML = window.screen.availHeight; document.getElementById("screen2").innerHTML = window.screen.availWidth; 
 <!DOCTYPE html> <html> <head> <title>My HTML</title> <meta charset="UTF-8"> </head> <body> <div id="screen1">You do not have javascript enabled.</div> <div id="screen2">You do not have javascript enabled.</div> </body> </html> 

Try this without return

document.getElementById("screen1").innerHTML = window.screen.availHeight;
document.getElementById("screen2").innerHTML = window.screen.availWidth;

Output from my pc

<div id="screen1">860</div>
<div id="screen2">1440</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM