tags:

views:

482

answers:

3

Hi, I have a div that is contain inside an iframe from a external html

<iframe src="test.html">

The html based "context menu" need to be on top of the iframe but the html code of the context menu is in the iframe itself. A pure CSS solution is preferred.

+2  A: 

The contents of an iframe cannot be displayed outside of the iframe. Within the iframe, you can have the div appear over other elements using "position: absolute" and "z-index: 1000" (or another appropriately large number). Note that absolutely positioned elements are removed from the flow of the document. The position can be set using the "top", "left", "right", and "bottom" CSS properties.

CalebD
A: 

As long as both frames are in the same domain, you could use Javascript to move the DOM element to the parent frame (window.parent.document.appendChild(...)). I have done this and it works. It's just a lot of hassle and you must decide yourself whether it's worth it.

Be sure to check out bobince's comment on the issue below on how to do it.

Don't forget the child frame's style sheets don't apply to the menu once you move it to the parent.

Pekka
We successfully executed something like this on two different domains but it was a bit more complicated.
Josef Sábl
I thought about suggesting this, but personally I feel it is a hack. If it is important that the content display outside of the confines of the iframe, it should not be in the iframe in the first place. It would be a clever solution though if it is the only solution.-
CalebD
I do think it's the only solution, and the question looks like a border case to me (like a drop-down or context menu). If you do the moving back and forth via the DOM, it's halfway clean and a legitimate solution if it's totally necessary for the user interface.
Pekka
You aren't supposed to be able to move a Node from one document to the other, it's specifically an error in DOM Level 1 Core though it does work in some browsers (sort of... I suspect it may cause some aggravating corner-cases and memory leaks). What you're supposed to do is call `document.importNode` to grab the node out of the iframe's document. Unfortunately this method isn't supported in IE... although it's not too tricky to write a fallback function to `createElement`, `createTextNode` etc. to copy the other document's node.
bobince
@bobince, great info. Thanks. I added a note to that effect to my answer.
Pekka
A: 

Your really duplicating typical "Ajax" functionality, since its already been abstracted by many frameworks (like jQuery), and you are not likely to do it any better I'd give that a shot. Don't reinvent the wheel.

You can find a great example here.

$("#links").load("/Main_Page #jq-p-Getting-Started li");
Joseph Silvashy