tags:

views:

1293

answers:

4

Hello, I am building a "universal wrapper" for several different external sites that would build a nav and footer with jquery and xml.

My problem: Since the script will be used on multiple sites that were built by other people, I need to build a container out just inside the opening and closing body tags. I have been able to target the body to prepend <div id="wrapper">, but I need to close the div somehow and trying to append only </div> does not work.

This is what I'm trying to do:

$(document).ready(function(){
    $("body").append("</div>");
});

My question: Is there a way to render </div> on its own?

+9  A: 

Have you looked at .wrapInner()? It will allow you to wrap all of the contents of <body></body> with a starting and ending tag:

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

That will result in:

<body>
  <div id="wrapper">
    <!-- original markup here -->
  </div>
</body>

Caution: One thing I noticed was the fact that jQuery will create the wrapper twice if the script is ran from within the body tags.

Jonathan Sampson
What's with the # in the ID?
rpflo
Oops. My Mistake. Thanks :)
Jonathan Sampson
+2  A: 

could you not use

$("body").wrapInner("<div id='wrap'></div>");
Josh
A: 

http://docs.jquery.com/Manipulation/wrapInner

Might be what you're looking for.

rpflo
A: 

Hi I am using this solution $("body").wrapInner(""); However I am writing an extension so the extension inserts a script in any webpage which executes the above code. The code doesnt work always and refreshes the page on same cases e.g. URL http://www.nytimes.com/2010/06/14/world/asia/14minerals.html?hp Can anyone please suggest an alternative ?

Sharad