+ 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; }
22 odpowiedzi
+ 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
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
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
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?
+ 1
No problem, but let me clarify something I use iPad running iOS 10.3.3 😢
+ 1
Wrap Script inside html and body (at last of body) and must use <script></script>
+ 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
+ 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.
+ 1
There is no body and you should use<script> and <\script> to end body
0
Gordon thank u.