What's wrong here ?? | SoloLearn: Learn to code for FREE!

+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; }

7/9/2020 9:38:43 AM

Mahi Chowdhury

26 Answers

New Answer

+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.

+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>

+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

+5

Great job, Ginfio and Nilesh

+5

Mahi Chowdhury Have a look at the attached code by me , Gordon and Ginfio :)

+3

Mahi Chowdhury You should have put a link to your code as there is a confusion if the js is inline or external.

+3

https://code.sololearn.com/Wa7S2eA2GHTI/?ref=app

+3

Gordon Thanks friend :)

+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

+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.

+3

ODLNT I guess I add that to my list of iOS app "features"?! Thanks for the heads up. 👍

+3

Use script tag

+2

It's fine. What is the error you getting 🤔

+2

Lol, I thought he placed in js block

+2

I see an onload attribute in the body tag. Was that edited in after the initial post?

+2

You should use <script>tag or use a javascript outside file If you select "I'll use a javascript outside file" to make a new javascript file, and add in html <script src = "index.js"></script> in html <head> tag please I recommend for u use a <script> tag. But I'm used to javascript outside file. So when you want use a <script>tag use it and when you want use a javascript outside file use it Thank you😊😊 Ps. If u it dosen't work,chat me pls

+1

Gordon why does your #2 not display in the console? 🤔

+1

No problem, but let me clarify something I use iPad running iOS 10.3.3 😢

+1

ODLNT i have a gen1 ipad and I just got a new iPad Pro. Night and day difference speed wise. The new iPadOS takes some getting used to, but it is fast. I just wish Apple would get it's act together with the browser engine. Even Chrome is forced to use their webkit, so it is crippled! 😭

+1

Wrap Script inside html and body (at last of body) and must use <script></script>