CancelImage Upload

How to change the CSS float property via JavaScript, including on Internet Explorer

Changing the float property dynamically is another one of those annoying instances where browser manufacturers could not agree with each other. While Netscape decided that the property should be accessed via cssFloat

document.getElementById("img1").style.cssFloat =setTo;

the creators of Internet Explorer were of a different opinion and decided it should be styleFloat.

document.getElementById("img1").style.styleFloat=setTo;

Good job for us developers then that we can actually request the current browser's type that the a user is surfing with via JavaScript. Upon detection you can tell the browser to use the appropriate float adjusting code. As you will probably already know there are three possible settings: left, right and none.

<html>
<head>
<title>How to change the CSS float property via JavaScript, including on Internet Explorer</title>

<script type="text/javascript">

function floatPosition(setTo)
{

      var browserName=navigator.appName;

      if(browserName=="Microsoft Internet Explorer")
      {

            document.getElementById("img1").style.styleFloat=setTo;

      }
      else
      {

            document.getElementById("img1").style.cssFloat=setTo;

      }

}

</script>

</head>
<body>

<img id="img1" src="image.jpg">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.
<br>
<input type="button" onclick="floatPosition('left')" value="Left">

<input type="button" onclick="floatPosition('right')" value="Right">

<input type="button" onclick="floatPosition('none')" value="None">

</body>
</html>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com