I'm currently reading Learning PHP, MySQL & JavaScript With jQuery, CSS & HTML5 4th edition by Robin Nixon.
I'm doing a JavaScript example involving adding an element to the DOM after acknowledgement of the alert dialog and then removing it upon another acknowledgement.
However, the page doesn't load properly in Chrome. The page continues to "load" while the alert is up. Saying it will add the object and while the alert is up saying it will remove the object but the text that should be there the whole time is not loaded until after the second alert box is closed.
The added element itself never loads at all in Chrome. Yet, the page works exactly has intended in both Edge and Firefox. Is there a setting in Chrome I need to adjust to allow the page to load properly or is this just how Chrome is currently designed to work?
The code in question is below as follows
<!DOCTYPE html>
<html>
<head>
<title>Adding Elements</title>
<script src='OSC.js'></script>
</head>
<body>
This is a document with only this text in it.<br><br>
<script type="text/javascript">
alert('Click OK to add an element')
newdiv = document.createElement('div')
newdiv.id = 'NewDiv'
document.body.appendChild(newdiv)
S(newdiv).border = 'solid 1px red'
S(newdiv).width = '100px'
S(newdiv).height = '100px'
newdiv.innerHTML = "I'm a new object inserted in the DOM"
tmp = newdiv.offsetTop
alert('Click OK to remove the element')
pnode = newdiv.parentNode
pnode.removeChild(newdiv)
tmp = pnode.offsetTop
function O(i) { return typeof i == 'object' ? i :
document.getElementById(i) }
function S(i) { return O(i).style }
function C(i) { return document.getElementsByClassName(i) }
</script>
</body>
</html>
Thank you for your help!
edit: There are 3 functions O, S, and C in the OSC.js file but I don't think they are the cause of any of the problems. I will post them below. Also added them to the snippet directly just so the code will work here. They are not in my actual code.
function O(i) { return typeof i == 'object' ? i :
document.getElementById(i) }
function S(i) { return O(i).style }
function C(i) { return document.getElementsByClassName(i) }