DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Randall has posted 3 posts at DZone. View Full User Profile

Dynamically Center A Flash Or Image File (including Window Resize)

12.28.2008
| 9761 views |
  • submit to reddit
        dynamically center a flash or image file (including window resize)

<html>
<head>
<script language="JavaScript">
		<!--
		var ie = document.all? true : false;
		function winSize() {
		    var myWidth = 0, myHeight = 0;
		    if( typeof( window.innerWidth ) == 'number' ) {
			  //Non-IE
			  myWidth = window.innerWidth;
			  myHeight = window.innerHeight;
		    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			  //IE 6+ in 'standards compliant mode'
			  myWidth = document.documentElement.clientWidth;
			  myHeight = document.documentElement.clientHeight;
		    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			  //IE 4 compatible
			  myWidth = document.body.clientWidth;
			  myHeight = document.body.clientHeight;
		    }
			
			if (myWidth > 900 || myHeight > 480)
			{
				//center flash on larger screens
				if (myHeight > 480) disp.style.marginTop = myHeight/2 - 240 + "px";
				else disp.style.marginTop = "0px";
				if (myWidth > 900) disp.style.marginLeft = myWidth/2 - 450 + "px";
				else disp.style.marginLeft = "0px";
			}
			else
			{
				//on 800x600 screen, put flash flush top/left
				disp.style.marginTop = "0px";
				disp.style.marginLeft = "0px";	
			}
		  }
			
		function init(){
			//display flash onload
			if (ie) disp.style.visibility = "visible";
			winSize();
		}

		onload = init;
		onresize = winSize;
		//-->
		</script>

	</head>

	<body>

		<div>
			<object style="position: absolute" id="flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width=900 height=480 align="middle">
				<param name="movie" value="file.swf"/>
				<param name="quality" value="best"/>
				<param name="play" value="true"/>
				<embed src="file.swf" width=900 height=480></embed>
			</object>		
			<!-- test with image, not flash --><!-- <img style="position: absolute" id="flash" name="loader" src="imagefilename" width="" height="" border="0" alt=""> -->
		</div>

	<script language="JavaScript">
	<!--
	var disp = document.getElementById("flash");
	if (ie)
	{		
		//hide while loading
		disp.style.visibility = "hidden";
	}
	else
	{
		//if not IE, move flash off screen until it loads
		disp.style.marginTop = "-2000px";
		disp.style.marginLeft = "-2000px";
	}
	
	//-->
	</script>

	</body>
</html>