简体   繁体   中英

Errors while calling a function in JavaScript

I am trying to make a time and date clock. I am getting all sorts of errors when trying to call a function:

<html>
<head>
    <title>Very Simple Clock By Ashton</title>
    <script>

        console.log("beginning creation of update function");

        function update() {
            document.getElementById('timeOutput').innerHTML = Date();
        }

        console.log("Successfully created update function");

        console.log("Calling the update function")

        update();

        console.log("successfully called the update function");
        </script>
</head>
<body>
    <p onload="update()" id="timeOutput">ERROR | Could Not Retrieve Time from server</p>
</body>

Body,Img,Iframe tags only have onload event p not supported.So Change with onclick event instead of onload

Only onload of p

 console.log("beginning creation of update function"); function update() { document.getElementById('timeOutput').innerHTML = Date(); }
 <p onload="update()" id="timeOutput">ERROR | Could Not Retrieve Time from server</p>

Change to click

 console.log("beginning creation of update function"); function update() { document.getElementById('timeOutput').innerHTML = Date(); } console.log("Successfully created update function"); console.log("Calling the update function") update(); console.log("successfully called the update function");
 <p onclick="update()" id="timeOutput">ERROR | Could Not Retrieve Time from server</p>

Onload does not work for <p> tags, so it is useless.

Enclosing update() inside window.onload() would allow it to work.

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