道 (Dào) ...

Wednesday, December 10, 2008

Embedding Flash into HTML

The problem: to embed Flash object into HTML page with alternative content provided for the case when Flash Player plug-in is not installed. Solution must work in IE6, IE7, Firefox and Opera browsers. First thing that everyone will want to use is <object> tag. But then he'll find out that its semantics is different for IE and other browsers. Code embedding Flash movie for IE looks like:

<object id="FlashAd" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" height="100" width="500">
<param name="movie" value="/resources/banner.swf">
<img src="http://www.blogger.com/resources/banner.gif" />
</object>

Where the movie parameters defines URL of a Flash movie, and <img src="http://www.blogger.com/static/alt_z_domu.JPG" /> represents the alternative content - GIF-image displayed in the place of the Flash movie in case of Flash Player absence. The code for FF is slightly different:

<object type="application/x-shockwave-flash" data="/resources/banner.swf" height="100" width="500">
<img src="http://www.blogger.com/resources/banner.gif" />
</object>

Here URL of the Flash movie is defined by the data attribute of the <object> tag. The alternative content is defined the same way. So the problem is how to combine this two <object> tags so that only one Flash movie will be visible. After googling for some time (it took me really long) I couldn't find working solution to the problem. Most of articles referred to the SWFObject Javascript library, which didn't want to work in my simple case (at least in IE 6). By the way, I think it's usage in the static case is not defensive at all. Rest of posts suggested to inject FF's <object> version into IE's one, so we get something like this:

<object id="FlashAd" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" height="100" width="500">
<param name="movie" value="/resources/banner.swf">
<object type="application/x-shockwave-flash" data="/resources/banner.swf" height="100" width="500">
<img src="http://www.blogger.com/resources/banner.gif" />
</object>
</object>

But next problem appears: there is no place for one common alternative content, which will be shown both in FF and IE. Another problem here is that both flash object's will be rendered by IE6.

The GoogleCode project for SWFObject suggested to use freaky conditional comments of IE:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" height="420" width="780">
<param name="movie" value="myContent.swf">
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="myContent.swf" height="420" width="780">
<!--<![endif]-->
<p>Alternative content</p>
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>

But I have found this approach broken, it works correctly only for FF, which can't comprehend conditional comments and reads them as normal comments, so finally it gave nothing.

So what is the solution? Solutions is to use conditional statements and the <comment> tag.

<object id="FlashAd" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="500" height="100">
<param name="movie" value="/resources/banner.swf">

<comment>
<object type="application/x-shockwave-flash" data="/resources/banner.swf" width="500" height="100">
<img src="/resources/banner.gif" />
</object>
</comment>

<!--[if gt IE 5]>
<img src="/resources/banner.gif" />
<![endif]-->

</object>

Only IE can understand both, other browsers are not aware of how to render <comment> tag, so they just step into it and render its inner HTML. One of the drawbacks of the described approach is the necessity to duplicate both <object> tag and the alternative content. But it works and that's why is good! :)

Wednesday, November 14, 2007

Every hour work routine can be more pleasant

Simple but handy utility programs that I use in my every day work and can suggest them for using:

Tuesday, October 30, 2007

Как установить свой Look&Feel?

На вопрос темы каждый бы мне ответил, что для установки своего вида графических элементов надо иметь JAR с классами, реализующими особый LaF, добавленный в classpath, и добавить в код метода main выполнение такого метода:

javax.swing.UIManager.setLookAndFeel("com.MyLookAndFeel");

где "com.company.CustomLookAndFeel" - имя класса ответственного за тот особый LaF.

Но что делать, если приложение уже собрано, а исходников нет, и разработчики не подумали о том, что мне не понравится вид по умолчанию (как Ocean в Java 5)? Или нужно, например, быстро и просто применить новый LaF?

На ответ наткнулся случайно (во время ночного серфинга по сети ;-)).
Итак, можно воспользоваться таким параметром JVM, как swing.defaultlaf, который можно указать через ключ -D во время запуска Java-приложения:
-Dswing.defaultlaf=имя_класса_LaF
! Не забываем про то, что jar с LaF должен быть в classpath :-)

Для удобства можно написать .bat (Win) или .sh (Linux) скрипт напободие:
javaw -cp "mainclasspath:lafclasspath" -Dswing.defaultlaf=LaFClass MainClass

Примеры custom LookAndFeel:
Как создать свой LookAndFeel:
P.S. Что касается меня, то я предпочитаю банальный системный LookAndFeel. ;-)

Blog Archive