0

Possible Duplicate:
How do I create an element after the closing body tag

Did a search, could not find this.

Is it possible to jQuery the closing </body> tag and then add some JS code just above? IE: find:

$("</body>") 

Then add some JS script just above this tag?

Community
  • 1
  • 1
Brian Johnson
  • 131
  • 2
  • 11
  • 5
    There is no such thing as a "closing tag" in the DOM. Tags, closing or otherwise, are a component of your markup, the DOM has only elements. – user229044 Feb 01 '13 at 19:57

4 Answers4

14

You don't need to select the 'closing' tag, just select the body and append what you want:

$('body').append('<h1>foobar</h1>');

update

As @meagar mentioned correctly, with jQuery you don't select a 'closing' tag, but the whole 'element', e.g. the body, or a 'paragraph' (p) as a whole

Trying to make it more clear, this code:

<body>
    <p>my paragraph</p>
    <script>
       $(body).append('<p>new paragraph</p>');
    </script>
</body>

Will produce this result:

<body>
    <p>my paragraph</p>
    <script>
       $(body).append('<p>new paragraph</p>');
    </script>
    <p>new paragraph</p>
</body>

The new paragraph is appended at the end of the body

thaJeztah
  • 27,738
  • 9
  • 73
  • 92
2

Just use .append().

$('body').append();
Dom
  • 38,906
  • 12
  • 52
  • 81
2

If you wanted to do that you would query for body and then use the append method.

$("body").append();
Brandon
  • 68,708
  • 30
  • 194
  • 223
1

To append content to the body use the following.

$('body').append(htmltoappend);

Is it possible to jQuery the closing tag and then add some JS code just above?

Why would you want to insert JS Code above the closing body tag?

Justin Bicknell
  • 4,804
  • 18
  • 26
  • OP misunderstood the jQuery selector, and thought you had to select the *closing* tag to add some content at the end of the body – thaJeztah Feb 01 '13 at 19:59
  • @thaJeztah - understood. But still, JS code is much different then html so I'm unsure if the OP is just trying to add js to page – Justin Bicknell Feb 01 '13 at 20:00
  • I've updated my answer, to clarify the result, hope this will help the OP to understand the workings of jQuery – thaJeztah Feb 01 '13 at 20:06