Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8217361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:26:16+00:00 2026-06-07T12:26:16+00:00

The following code uses DOMNodeInserted to perform DOM manipulation on the body element once

  • 0

The following code uses DOMNodeInserted to perform DOM manipulation on the body element once it is inserted into the DOM. I know $(document).ready(function { }); is enough to do the job, but I want to test the DOMNodeInserted event.

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
        function DOMmanipulation() {
            if (document.body) {
                document.removeEventListener('DOMNodeInserted', DOMmanipulation);
                // DOM manipulation start
                document.body.innerHTML = '<div class="wrapper">' + document.body.innerHTML + '</div>';
                // DOM manipulation end
            }
        }
        if (document.addEventListener) {
            document.addEventListener('DOMNodeInserted', DOMmanipulation);
        }
    </script>
</head>
<body>
    <p>Lorem ipsum dolor sit amet.</p>
</body>
</html>

As you can see, the DOM manipulation is to wrap all innerHTML of body into <div class="wrapper"></div>.

It does work. However, a weird thing happened. If you use Firebug or Chrome Developer Tool to inspect the manipulated body, you can find that there was a mysterious element being added. It is:

<div style="padding: 0px; margin: 1px 0px 0px; border: 0px none; visibility: hidden; width: 0px; height: 0px; position: static; top: 0px;"></div>

And despite the success of the manipulation, Firebug shows this error:

  • Node was not found (jquery.min.js line 2)

And Chrome Developer Tool shows this error:

  • Uncaught Error: NOT_FOUND_ERR: DOM Exception 8 (jquery.min.js line 2)

But if the manipulation is just adding className, there is no error:

document.body.className += ' js';

The most weird thing is, if you remove the jQuery reference, the event won’t work. Does that mean the native DOMNodeInserted event sometimes needs jQuery to work?

By the way, if the DOM manipulation is using jQuery syntax:

$('body').wrapInner('<div class="wrapper"></div>');

Then the mysterious element becomes more weird:

<div style="padding: 0px; margin: 1px 0px 0px; border: 0px; visibility: hidden; width: 0px; height: 0px; position: static; top: 0px; zoom: 1; ">
    <div style="position: absolute; top: 0px; left: 0px; width: 1px; height: 1px; padding: 0px; margin: 1% 0px 0px; border: 0px; visibility: hidden; ">
        <div style="position: relative; top: 0px; left: 0px; width: 1px; height: 1px; padding: 0px; margin: 0px; border: 5px solid rgb(0, 0, 0); display: block; overflow: hidden; ">
            <div style="padding: 0px; margin: 0px; border: 0px; display: block; overflow: hidden; "></div>
        </div>
        <table style="position:absolute;top:0;left:0;width:1px;height:1px;padding:0;margin:0;border:5px solid #000;" cellpadding="0" cellspacing="0"><tbody><tr><td></td></tr></tbody></table>
    </div>
</div>

To sum up, we can see three issues from the above experiment:

  • weird element(s) being added
  • an error occurred despite the success of the DOM manipulation
  • DOMNodeInserted sometimes seems to need jQuery to work

Any explanation would be appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:26:18+00:00Added an answer on June 7, 2026 at 12:26 pm

    jQuery starts some tests when it has been loaded (line 1537 in the un-minified version)

    // Run tests that need a body at doc ready
    jQuery(function() {
    var container, outer, inner, table, td, offsetSupport,
    ...
    

    For this it adds a div element with some stylings. That is your mysterious element you’ve found.

    Because of your event handler, this element gets wrapped within another div. At line 1654 jQuery wants to remove their test element with

    body.removeChild( container );
    

    which failes because the container isn’t a child element of body anymore.

    Edit
    In your example you’re not adding an element to the DOM so the event can’t fire 😉

    window.onload = function() {
        document.body.appendChild(document.createElement("div"));  // will fire DOMNodeInserted
    }
    

    Why it “works” with jQuery? Because jQuery adds an element to the DOM (the mysterious test element) 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have the following code which uses knockout.js, var allJobberDetailsArray = []; getAllJobberDetailsArray();
I have the following code which re-uses a CookieContainer which logs in on the
My website uses the following code: $a1=1293011062; // hex code umm i think !
The following code gets a 'report line' as an array and uses fputcsv to
I have the following code, which uses a stream to open and modify an
The following code is used in a query for fetching records. It uses the
I have the following code to scroll images in vertical direction (uses jCarouselLite )
I have the following code, in which Boost.Local uses a function callback to load
The textbook I'm using to learn JavaScript uses the following code to display an
I'm dealing with a large code base that uses the following construct throughout class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.