Nir
Nir

Reputation: 25369

What happens when two libraries try to change the same dom element at the same time

Say we have 2 javascript scripts trying to change the same DOM element. One tries to enter html into a div and the other tries to move the div elsewhere on the page (for A/B testing) and we cant predict when they will finish loading and run. Can it mess up the html page? Do the browsers know to prevent that?

Upvotes: 0

Views: 62

Answers (2)

Luca Matteis
Luca Matteis

Reputation: 29267

You can't really call something at the same time. JavaScript isn't multi-threaded and you can only do one thing at a time.

Your example deals with the DOM which is not really related to the JavaScript engine but the DOM engine (which is multi-threaded). You can do many things concurrently in the DOM, however, since JavaScript is the interface to the DOM, JavaScript will not let you call two DOM related functions at the same time - one will always be called before the other, even though they happen concurrently inside the DOM memory space (not JavaScript's).

Upvotes: 2

Piskvor left the building
Piskvor left the building

Reputation: 92772

Well, JavaScript is single-threaded, so although it may seem that both are executing at the same time, they aren't really. If they're modifying the same element, sooner or later one will try to modify something that doesn't exist. This case, however, will be no different from any other attempt to change a nonexistent DOM element. The browsers won't try to prevent this (after all, your code tells them to do it).

Upvotes: 0

Related Questions