Introducing Dynamic favicons in JSF applications

0

Being in New Orleans for a festive conference like ODTUG brings me in a mood to write about something colorful – even if not very useful. And perhaps every now and again you may find it useful after all. You probably know of favicons – if not the word itself. Websites can define a favicon – a small icon that will display on the tab and/or the location bar of your browser (if the browser supports it, which not all browsers do). The favicon is a 16×16 icon that is typically referenced from a link element in the web page. You can use a site-wide favicon by placing a favicon.ico file – with exactly that name – in the web root of the site (just like the index.html file). 

 

In this article, I will demonstrate how easy it is to add dynamic – programmatically set – favicons. ....
This allows you to have the favicon depend on dynamic conditions, such as the data currently displayed on the page or even the time of day. It also allows you to allow your visitors and end users to select their own favicon if they feel so inclined. All it takes is a little JavaScript library and a simple call in each page. 

Here we see a simple example of a Departments overview page for which the favicon has been set to a cute little home icon:

When we select an employee who is a manager and then press the Employee Details button, we are taken to the Employee page with a manager-like icon displayed as favicon:

When we select an employee with a different job or change the job of the selected employee and press submit, the page is displayed with a ‘normal employee’ favicon:

 

This functionality is implemented in very simple way. The departments page has this script element included:

 &lt;trh:script source=&quot;favicon.js&quot;/&gt;<br /> &lt;trh:script text=&quot;addLink ('#{'department.ico'}')&quot; /&gt;&nbsp;

And the Employee page this slightly more complex element that uses an EL Expression to determine the icon based on the Job for the current employee:

 &lt;trh:script source=&quot;favicon.js&quot;/&gt;<br /> &lt;trh:script text=&quot;addLink ('#{bindings.Job.inputValue=='MANAGER'?'manager.ico':'empicon.ico'}')&quot; /&gt;<br />&nbsp;

Both pages include a simple JavaScript library that does the dynamic assignment of the favicon.

// based on http://softwareas.com/dynamic-favicon-library-updated by Michael Mahemoff's <br /><br />function addLink (iconURL) {<br />  var link = document.createElement(&quot;link&quot;);<br />  link.type = &quot;image/x-icon&quot;;<br />  link.rel = &quot;shortcut icon&quot;;<br />  link.href = iconURL;<br />  removeLinkIfExists();<br />  document.getElementsByTagName(&quot;head&quot;)[0].appendChild(link);<br />}<br /><br />function removeLinkIfExists() {<br />  var links = document.getElementsByTagName(&quot;head&quot;)[0].getElementsByTagName(&quot;link&quot;);<br />  for (var i=0; i&lt;links.length; i++) {<br />    var link = links[i];<br />    if (link.type==&quot;image/x-icon&quot; &amp;&amp; link.rel==&quot;shortcut icon&quot;) {<br />      document.getElementsByTagName(&quot;head&quot;)[0].removeChild(link);<br />      return; // Assuming only one match at most.<br />    }<br />  }<br />}<br />&nbsp;

This library is based on the work by Michael Mahemoff (see http://softwareas.com/dynamic-favicon-library-updated  )

Resources

Site to generate favicons from just about any image format: http://www.html-kit.com/favicon/  

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

Comments are closed.