26
I’d like to understand why, when we use the functions alert, confirm or prompt, they always cause a kind of "freeze" on the screen.
For example, I have a alertand then I have a div with a defined style. The style of div will only be visible after I close the alert.
<script>alert('oi')</script>
<div style="background-color: black;height: 50px">
</div>
I thought it was about order as things were defined, but it doesn’t seem like that:
<div style="background-color: black;height: 50px"></div>
<script>alert('oi')</script>
I’ve also noticed that in other cases, when you have a dynamic definition of creating elements, the same thing happens.
In the example below, I already have one element created, however, after a while I create another dynamically and then call the function confirm.
setTimeout(function () {
var div = document.createElement('div');
div.innerHTML = 'Nova div';
document.body.appendChild(div)
confirm('oi')
}, 200)
<div style="background-color: black;height: 50px"></div>
The result is that even I define document.body.appendChild before confirm, the appendChild appears to be running only after the closing of confirm.
Why Javascript’s native dialogs "lock" the process of creating elements and style settings?
Since sometimes this causes an unwanted "whiteness" on the screen, as I might call it
alert,confirmorpromptwithout interfering with page loading?
But why, in the second case, the
alertis displayed before the DIV?– Jéf Bueno
@jbueno has to do with how the browser assemble the page, I added an example.
– Sergio
@Sergio Aproveitar, in this update of your example i change the color by JS itself. But it still doesn’t change the color. It’s the same reason to render HTML first?
– Randrade
@Exact Randrade. Javascript already knows that the color has changed but the browser has not yet come to render the page.
– Sergio
1 year passed and nobody noticed the "dinaussauros" né @Sergio :D
– Wallace Maxters
@Wallacemaxters :o
– Sergio