views:

471

answers:

4

Im trying to make a pop-up like window using jquery and its modal box. First I load the content from a html file:

$("#test").load("test.htm");

Then I load the popup:

$("#test").dialog("open");

This works like it should, the content of test.html is injectet into the modal pop-up. There is only one think that is wrong, and that is the BODY tags are gone from the source of the pop-up. I need the BODY tag to be there because I do some formatting based on the BODY tag.

Does anyone know why jQuery.Load() removes the BODY tag? And are there any workarounds?

A: 

You might dynamically create the body tag using document.write of js as an alternative.

Sarfraz
This is going to be dynamic later, meaning I'm going to fetch the HTML code from an XML-file and load it into the DIV using an ASP.NET handler (ashx-file).So I cant really add the body tag using javascript since I dont know on which lines to add it since the document im going to load might change.
Martin
+1  A: 

A page can only have one body tag. If you already have one on the page, the second will be ignored.

In your case, it sounds like the browser is ignoring the duplicate body (nothing specific to jquery). Rather than use the body for styling, use a containing <div> with an id or class which will be retained.

Jonathan Fingland
Sounds reasonable, I guess that sadly is the answer..
Martin
+1  A: 

It probably removes the body tag because it's not allowed! Each document can only have one body. Rather than force everyone to redo all their HTML pages, jQuery probably just grabs the contents of the body to use when you call load().

Have you thought about perhaps wrapping everything in a containing element? eg: <div class="body"> You can then apply the exact same styles to that element.

/* change this: */
body { color: #f0f; etc }

/* to this: */
body, div.body { color: #f0f; }
nickf
+1, perfect example, and requires the least work of all possible solutions.
Duroth
This is a good answer. Perhaps it would be improved by using the dialog's class (from memory, its ui-dialog), although that depends on whether he's using dialogs for any other reason. I get the feeling he isn't.
Gausie
A: 

You are loading the HTML into an existing document that already has a body tag. A document can only have one so it automatically filters anything and extracts only the HTML inside the body tag when using load. You should wrap your HTML in a div with a specific class and do your formatting based on that class.

From the load docs (emphasis mine):

In jQuery 1.2 you can now specify a jQuery selector in the URL. Doing so will filter the incoming HTML document, only injecting the elements that match the selector. The syntax looks something like "url #some > selector". Default selector "body>*" always applies. If the URL contains a space it should be escape()d. See the examples for more information.

tvanfosson