Óliver Máni Óliver Máni - 6 months ago 21
Javascript Question

Why is the onclick event on the body element not working?

I'm trying to send users to another page when click html body:

JavaScript

c.js
:

function clickBody(){
window.location.href = '/';
}


HTML:

<!DOCTYPE html>
<html>
<head>
<script src="c.js"></script>
</head>

<body onclick="clickBody();">
</body>
</html>


I can run the function from console, but clicking the
<body>
is not working.

Answer

<body> element have no width and height,add something like bellow

<!DOCTYPE html>
<html>
<head>
      <style>
        body{
            width: 100%;
            height: 500px;
        }
       </style>
 <script src="c.js"></script>
</head>

<body onclick="clickBody();">

</body>  
</html>
Comments