+ 2

What's wrong here ??

<!DOCTYPE html> <html> <head> </head> <body onload="startTime()"> <div id="txt"></div> </body> </html> function startTime() { var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); var s = today.getSeconds(); m = checkTime(m); s = checkTime(s); document.getElementById('txt').innerHTML = h + ":" + m + ":" + s; var t = setTimeout(startTime, 500); } function checkTime(i) { if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10 return i; }

9th Jul 2020, 9:38 AM
Mahi Chowdhury
Mahi Chowdhury - avatar
26 Answers
+ 8
You should use <script> </script> tags to embed your javascript in your html code. you can put these tags in either body or head.
9th Jul 2020, 9:44 AM
Rimjhim
Rimjhim - avatar
+ 6
While writing the question, click on the ‘+’ sign next to where you’re writing. Then select ‘insert code’. That’ll make it easier for you (instead of having to copy and paste the whole code). umm, with the code, you can add a script tag inside the body instead of outside. ... <body> my html code here ... <script> my... JS... code... here... </script> </body>
9th Jul 2020, 9:51 AM
Ginfio
Ginfio - avatar
+ 6
https://code.sololearn.com/W70FZ7cM02vy/?ref=app See this Use the script tag and insert your JS and close the document with </body> and </HTML> you have places them in the wrong place
9th Jul 2020, 9:54 AM
Nilesh
Nilesh - avatar
+ 5
Great job, Ginfio and Nilesh
9th Jul 2020, 10:01 AM
Gordon
Gordon - avatar
+ 5
Mahi Chowdhury Have a look at the attached code by me , Gordon and Ginfio :)
9th Jul 2020, 1:26 PM
Nilesh
Nilesh - avatar
+ 3
Mahi Chowdhury You should have put a link to your code as there is a confusion if the js is inline or external.
9th Jul 2020, 9:47 AM
Saphal Poudyal
Saphal Poudyal - avatar
+ 3
Gordon Thanks friend :)
9th Jul 2020, 10:02 AM
Nilesh
Nilesh - avatar
+ 3
Mahi Chowdhury Just in case what you means is putting scripts in JS tab and getting an "cannot read property of null" error. That will be because scripts in JS tab are put in <head> and runs before the DOM exists. One of the solution is to use a window.onload anonymous function to contain all JS codes. https://code.sololearn.com/WooxNg5GU7XK/?ref=app
9th Jul 2020, 10:09 AM
Gordon
Gordon - avatar
+ 3
Code Crasher You don't see #2 because you are using an iOS device. For some reason, you can't use console.log() in the HTML tab on iOS devices.
9th Jul 2020, 10:43 AM
ODLNT
ODLNT - avatar
+ 3
Use script tag
11th Jul 2020, 7:45 AM
Master Genius [ INACTIVE ]
Master Genius [ INACTIVE ] - avatar
+ 2
It's fine. What is the error you getting 🤔
9th Jul 2020, 9:43 AM
v@msi😉
v@msi😉 - avatar
+ 2
Lol, I thought he placed in js block
9th Jul 2020, 9:45 AM
v@msi😉
v@msi😉 - avatar
+ 2
I see an onload attribute in the body tag. Was that edited in after the initial post?
9th Jul 2020, 10:15 AM
ODLNT
ODLNT - avatar
+ 1
No problem, but let me clarify something I use iPad running iOS 10.3.3 😢
9th Jul 2020, 10:47 AM
ODLNT
ODLNT - avatar
+ 1
Wrap Script inside html and body (at last of body) and must use <script></script>
10th Jul 2020, 5:23 AM
Shrish
Shrish - avatar
+ 1
in my experiwnce do not use your script code inside the html code. you can use seperate script file and import it to you html page and call function appropriatly. other part is there is no script tags you have inserted this code
10th Jul 2020, 3:04 PM
Gayan Suranga
Gayan Suranga - avatar
+ 1
This code does not contain <script> tags. JavaScript inserted in an HTML document in this way, it won't work if you don't insert it between <script> tags. I hope I can be of use to you.
10th Jul 2020, 4:23 PM
joseffie
joseffie - avatar
+ 1
There is no body and you should use<script> and <\script> to end body
10th Jul 2020, 7:09 PM
Abdulkareem Aremu
Abdulkareem Aremu - avatar
0
Gordon thank u.
9th Jul 2020, 10:01 AM
Ginfio
Ginfio - avatar