Wrap it in $(document).ready(function(){ ... });
Thus, jQuery will start it only after the page is fully loaded.
If you do not, the code will be executed as soon as possible, which may be before loading the DOM, so you may not have a body element to add a class.
The fact that some browsers work, while others do not imply that different browsers (a) load the page at different speeds and / or (b) perform the initial loading tasks in a different order. But you should not worry about it. Just call $.ready() and it will all be done in the correct order for jQuery for you.
source share