Wątek przeniesiony 2022-12-19 17:34 z JavaScript przez Riddle.

Wyświetlenie rozmiaru okna przeglądarki

0
<!DOCTYPE html>
<html>

<body onload="checkResolution()">

Your screen width: <element id="Show-Width"><br>
Your screen height: <element id="Show-Height">

<script>
  function checkResolution() {
    var screenWidth = window.screen.width * window.devicePixelRatio;
    document.getElementById("Show-Width").innerHTML = screenWidth;
    
    var screenHeight = window.screen.height * window.devicePixelRatio;
    document.getElementById("Show-Height").innerHTML = screenHeight;
  }
</script>
</body>
</html> 

Dlaczego funkcja pokazuje mi tylko element Show-Width, a ignoruje całą Show-Height? Może wiecie. Hmm... :-) Dziękuję!

Kuba

0

Błąd jest taki, że <element id='..'> nie ma domknięcia

możesz zmienić na taki

<!DOCTYPE html>
<html>

<body onload="checkResolution()">

Your screen width: <element id="Show-Width"> </element><br>
Your screen height: <element id="Show-Height"></element>


</body>
</html> 

innerHTML to jest miejsce pomiędzy otwarciem elementu, a zamknięciem: <a>.... tutaj ...</a>

0
<!DOCTYPE html>
<html>

<body onload="checkResolution()">

Your screen width: <element id="Show-Width"></element>
<br>
Your screen height: <element id="Show-Height"></element>

<script>

function checkResolution() {

  var screenWidth = window.screen.width * window.devicePixelRatio;
  document.getElementById("Show-Width").innerHTML = screenWidth;
  
  var screenHeight = window.screen.height * window.devicePixelRatio;
  document.getElementById("Show-Height").innerHTML = screenHeight;
  
}

</script>
</body>
</html> 

Ok! Kodzik w tej wersji działa. Ale mam pytanie. Generuje mi to takie wartości: Jak je zaokrąglić?

Your screen width: 1365.6716417910447
Your screen height: 768.3582089552239

Kupić większego laptopa? :-DDD

1

Domyślnie w HTMLu nie ma takiego znacznika jak <element>, więc tutaj masz pewnie drugi błąd (chociaż można go stworzyć).

0
Kubutek napisał(a):
<!DOCTYPE html>
<html>

<body onload="checkResolution()">

Your screen width: <element id="Show-Width"><br>
Your screen height: <element id="Show-Height">

<script>
  function checkResolution() {
    var screenWidth = window.screen.width * window.devicePixelRatio;
    document.getElementById("Show-Width").innerHTML = screenWidth;
    
    var screenHeight = window.screen.height * window.devicePixelRatio;
    document.getElementById("Show-Height").innerHTML = screenHeight;
  }
</script>
</body>
</html> 

Dlaczego funkcja pokazuje mi tylko element Show-Width, a ignoruje całą Show-Height? Może wiecie. Hmm... :-) Dziękuję!

Kuba

Zobacz co siedzi w innerHTML elementu Show-Width:

console.log(document.getElementById("Show-Width").innerHTML);
<br>
Your screen height: <element id="Show-Height">

<script>
  function checkResolution() {
    console.log(document.getElementById("Show-Width").innerHTML);
  }
</script>

 </element>

I potem ten innerHTML nadpisujesz, a tym samym usuwasz wszystkie te elementy.

Odpowiedzią na to jest domknięcie </element>.

0

Dzięki za pomoc. Chcę się zacząć uczyć JavaScript. Ale nie wiem za co się złapać na początku. Może mi doradzicie od czego mam zacząć? Dzięki. Miłego dnia. Kuba

0
Kubutek napisał(a):

Dzięki za pomoc. Chcę się zacząć uczyć JavaScript. Ale nie wiem za co się złapać na początku. Może mi doradzicie od czego mam zacząć? Dzięki. Miłego dnia. Kuba

No moim zdaniem raczej nie od webowki, tylko od node'a. Jakieś gry konsolowe albo aplikacje które operują na plikach.

1 użytkowników online, w tym zalogowanych: 0, gości: 1