Link to home
Start Free TrialLog in
Avatar of mxyztplk333
mxyztplk333

asked on

Position Floating Div respect to top&left of window document

I am trying to make a cross-browser floating div that "floats" on top of the window, with absolute position of  left-150px top-200px.  I got it working in Firefox with the following code:

var rng=document.createRange();  
rng.selectNode(document.body);  
document.body.appendChild(rng.createContextualFragment(myDiv));

where myDiv is set to have absolute position style that i need:

style="position:absolute;top:200px;left:150px;"

I can't get this to work on IE.  Anyone know the IE version of the code that I have above for Firefox?  Basically, appending it to the body and setting the position to some absolute position(may have to set the z-index as well).  Any suggestions/help will be greatly appreciated.

Thank you.  
Avatar of Lakio
Lakio
Flag of Iceland image

Send a screenshot of it in Firefox.
absolute positioning tends to give cross browser problems, each browser interprets the starting point for the absolute somewhat differently, depending on the elements in the table.  Try both IE6 and 7, see what difference there is.  If the problem is just in IE6 (which it usually is) I wouldn't worry too much about it.  Also test on the MAC browsers for full X-browser compat.
Avatar of mxyztplk333
mxyztplk333

ASKER

Thank you for your replies.  Basically, it's just a floating div with one Flash object&embed tags.  I know how to attach this Div to the document body in Firefox, but I don't know how to manipulate the DOM in IE and append this div to the top window BODY tag.  Please let me know if you can provide some insight.  

Lakio, would you still find the firefox screenshot helpful after my clarification?   Thanks for all your help.
I made a small page, try it and tell me if thats want you want or/and send me a screenshot ;)

.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Lakio made this</title>
	<script>
 
	function createDomElement( tag, where, text ) //  createElement ( 'div', document.body, 'The text in the div',  'Attribute1', 'Attribute1value',  'Attribute2', 'Attribute2value',  'Attribute3', 'Attribute3value'..... and so on )
	{
		with(document)
		{
			var element = createElement( tag );
			var a = arguments;
			
			for( var i=3; a[i]; i++ )
			{
				element.setAttribute( a[i++], a[i] );
			}
 
			text && element.appendChild( createTextNode(text) );
			where.appendChild( element );
			return element;
		}
	}
	
	function addSuperDiv( linkElm )
	{
		var element = createDomElement( 'div', document.body, 'The text in the div asdf asdfasdf asdf asdfa sdf ',  'id', 'superDiv', 'style', 'position:absolute; background:blue; top:80px' );
		element.style.left = '40px';
		
		linkElm.parentNode.removeChild( linkElm ); // deletes the link
	}
 
	</script>
</head>
<body>
	qwertyuiopðasdfghjklæ´zxcvbnm,.<br />
	qwertyuiopðasdfghjklæ´zxcvbnm,.<br />
	qwertyuiopðasdfghjklæ´zxcvbnm,.<br />
	qwertyuiopðasdfghjklæ´zxcvbnm,.<br />
	qwertyuiopðasdfghjklæ´zxcvbnm,.<br />
	qwertyuiopðasdfghjklæ´zxcvbnm,.<br />
	qwertyuiopðasdfghjklæ´zxcvbnm,.<br />
	qwertyuiopðasdfghjklæ´zxcvbnm,.<br />
 
	<a href="#" onclick="addSuperDiv(); return false">TEZT</a>
</body>
</html>

Open in new window

I forgot "this":
<a href="#" onclick="addSuperDiv( this ); return false">TEZT</a>
Have you tried putting a z-index on it to make it independent of the rest of the page?
ASKER CERTIFIED SOLUTION
Avatar of Lakio
Lakio
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, Lakio!  I really appreciate your help on this.