why this line is not executed? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why this line is not executed?

I am going to print js code"documents. bgColor=" blue"" in the head section of html through script tag but the background color not changing to blue. Does anyone has any idea https://code.sololearn.com/WoS33gFb21Z7/?ref=app

31st May 2021, 3:09 PM
Ur-Wellwisher
Ur-Wellwisher - avatar
5 Answers
+ 5
The following line runs almost immediately after you set it to blue: document.bgColor="green" Remove that line and it'll be white initially because the body element is added after your bgColor = "blue". The new body element won't pick up your document.bgColor. Remove that bgColor = "green" and remove the body tag and you'll get the blue background you expect. All this is relying on quirks mode in the browser so the results may be different from one browser to another. If you wanted this for a website intended to impress people, I wouldn't change background colour like you are. I'd use CSS like most websites do.
31st May 2021, 3:13 PM
Josh Greig
Josh Greig - avatar
+ 1
It looks like you swapped the following 2 lines since the last time I looked at your referenced code: document.write("hello") document.bgColor="blue" Setting to blue after writing "hello" is important since that gets the browser to add the body element in the browsers I tested. Can you try this? I tested the following in Microsoft Edge, Google Chrome in Windows and the Sololearn Android app and it goes blue in all that I tested. https://paste-and-share.com/en/NjBiNTA0ZTM2NDAzMmIwZTI0MTk2OWI4 I would have pasted the HTML here but Sololearn is blocking the HTML on me.
31st May 2021, 3:47 PM
Josh Greig
Josh Greig - avatar
+ 1
Sumith Thota rather than puting your code inside head, put it in body: script run as soon they are encountered while parsing ^^ rather of using deprecated attribute bgColor, use style attribute (with css property background-color)... https://code.sololearn.com/Wnfnsa9F9TEr/?ref=app I've put one of your two buttons before script directly as html element... and some text after the script in html to let you better view that code is executed in between (where it is placed)... also, better practice to not rely on automatic semi-colon insertion at end of lines in JS, and enclose attribute values by quotes in HTML ;)
31st May 2021, 8:35 PM
visph
visph - avatar
0
I tried. But it not worked at
31st May 2021, 3:19 PM
Ur-Wellwisher
Ur-Wellwisher - avatar
0
Thank you
31st May 2021, 4:07 PM
Ur-Wellwisher
Ur-Wellwisher - avatar