Jul
28
2009

Implementing iFrame Toolbar

Problog

this post covers the following topics:

  • definition and usage of iFrame toolbars
  • The 100% height bug for iframe tag
  • Using an auto-resize for an iframe
  • writing an iframe toolbar demo

Lately i see more and more web sites using the iframe toolbar technique. This technique allows a web site to present content from a different site using an iframe element <iframe>, while still having a small part of the browser displaying a small toolbar at the top. This toolbar allows the site to interact with its users while they consume external content.

There are several usage for this technique. These are the main ones that i encountered:

Social Networks

Using the toolbar, social network sites can provide interaction and content to their users while they browse to external links outside of their site. I think most of the big social networks, like digg, linkedin and facebook have implemented this technique into their sites.

Social Networks using iframe toolbar

Social Networks using iframe toolbar

Search Engines

The iframe toolbar is commonly used in image search engines, where clicking on an image search result opens the original site of the image within the search engine page. This technique allows the search engine to increase interaction and minimize traffic lost.

Search engines using iframe toolbar

Search engines using iframe toolbar

Redirect Services

Redirect services, or should i use their sexy name -url shortners – are also a valid market for using the iframe toolbar technique. Usually when such a service implements this technique it allows its users to perform social interaction with the url it redirected (like tweet it, or share it on facebook).  The ow.ly service is a good example of such implementation

ow.ly using iframe toolbar

ow.ly using iframe toolbar

When i thought how it’s being implemented, i automatically thought about the most simple solution: setting a page with a header div and an iframe tag that has height:100%. Unfortunately, it doesn’t work that way – apparently there is some bug with the 100% height.  Since this technique is becoming so popular i thought it might be interesting to share how its being implemented.

iFrame Toolbar – The Problem

As i mentioned above, setting the height of the iframe to 100% in the css is not going to work. i searched a bit and found that its a well know bug. I oreder to demonstrate how the workaround works i decided to implement a demo toolbar that implements the following functionalities:

  • The toolbar page will get the url for the iframe as a parameter (www.mytoolbar.com/?iframe=URL)
  • Will allow the user to tweet about the url
  • will allow the user to search Google
  • Provide a back link
  • Will allow the user to close the toolbar and to load the iframe as the main page
  • will allow the user to expand the toolbar to get more functionality and content

You can see and play with the toolbar in this demo page.

Now lets see how its really works.

iFrame Toolbar – The HTML

There is nothing interesting here that i think worth talking about. Basically, i divided the screen into two sections (divs): one is the toolbar and the other is for the iframe. Off course the elements inside the toolbar div can be changed according to each need.


<body>
	<div id='toolbar'>
		<div id='search'>
			<input id='qs' type='text' size='35' ></input>
			<input type='button' value='search' id='searchBtn' ></input>
		</div>
		<div id='main'>
			<div id='links'>
				<a id='back' href='javascript:void(0);'>Go back</a>
				<a id='twitter' href='javascript:void(0);'></a>
				<a id='arrow' href='javascript:void(0);'></a>
				<a id='close' href='javascript:void(0);'></a>
			</div>
			<div id='extra'>
			<br>
			Is that alright? Give my gun away when it's loaded <br>
			Is that alright? If you don't shoot it how am I supposed to hold it <br>
			Is that alright? Give my gun away when it's loaded <br>
			Is that alright With you?
			</div>
		</div>

	</div>
	<div id='iframe'>
		<iframe src='' />
	</div>
</body>

iFrame Toolbar – The CSS

As in the HTML section, there is no exciting code to report here as well. Anyone can (and from looking at my design skills – MUST) change the css to match it to their own design  and look-and-feel. However, i think its important to mention the followings:

  • The following deceration – html {overflow: hidden;} – is important since we don’t need the scrollbars in our toolbar page, and it also fixes an IE bug, wher it displays the scrollbars even when they are not being used in a disabled mode.
  • Since in my example i allow the user to expand the toolabr i aslo declared the toolbar div to hide the content in case of an overflow.
  • Though its not working, i set the iframe and the div tag’s height to 100% – just as an decleration of what i expect of it to do (optemistic coder…)

html {overflow: hidden;} /*we don't need any scrolls for our html */

#toolbar
{
	background: url(bck.png) repeat-x transparent;
	height: 25px;
	overflow: hidden;
}

...

#iframe { overflow: hidden;} /*this is to remove the scroll when not needed*/

#iframe, iframe
{
	width: 100%;
	height: 100%;
}

iFrame Toolbar – The JavaScript

For my own convinience i used jquery in my code, although for this demo its not neccessary and even considered an overload to such a project.  I’ll go over the most important functions that hadle the iframe resizing, while i’ll leave you to read the other non-related code – which i think are self-explanatory.

main

This function extract the url parameter from the page url using the getIframeURL function. Then it register to some UI events and finnaly i call the most important function: resizeIframe()


$(document).ready(main);

function main()
{
	$('iframe').attr('src', getIframeUrl() );
	registerEvents();
	resizeIframe();
}

function getIframeUrl()
{
	var url = window.location.href;
	var iframe_url = 'http://www.nowaythatthereissomethinghere.com';
	var param_start = url.indexOf("iframe=");
	if( param_start != -1 )
		iframe_url = url.substr(param_start+7,url.length-param_start-7);
	if( iframe_url.indexOf("http://") == -1)
		iframe_url = "http://" + iframe_url;

	return iframe_url;
}

resizeIframe

In order to solve the 100% height bug i’m going to calculate the exact height that i need to set teh iframe. to do so, i’ll use the following logic:

iframe height = window height – toolbar height

I know its not obvious from its name but the windowHeight function return the height of the window (wow, you didn’t expect that, didn’t you?), while the getObjectHeight returns the height of the object its gets as a parameter. So if we subtract the second function from the first we get the height of the iframe.


function resizeIframe()
{
	$("#iframe").height( WindowHeight() - getObjHeight(document.getElementById("toolbar")) );
}

function WindowHeight()
{
	var de = document.documentElement;
	return self.innerHeight ||
		(de &amp;amp;amp;&amp;amp;amp; de.clientHeight ) ||
		document.body.clientHeight;
}

function getObjHeight(obj)
{
	if( obj.offsetWidth )
	{
		return obj.offsetHeight;
	}
	return obj.clientHeight;
}

Since we need the iframe height to be calculated every time the window size is changed, we also need to register to the window resize event as so:


$(window).resize( function() {resizeIframe();} );

That’s it, that’s all there is to it. All the rest of the code is to handle the UI functionality that i decided to implement for this demo toolbar. just want to clear some points:

  • Due to security reasons the browser prevent access from reading an iframe source url, so if the user has clicked a few links in the ifrmae, there is no way to find out which page is being displayed in the iframe.
  • I used the jquery animation method in order to expand and collapse the toolbar.

You can see and play with the toolbar in this demo page. if you want to change the iframe url, just change the iframe parameter to whatever you want, like so: http://www.amirharel.com/labs/it/res_iframe.html?iframe=YOUR URL

Bookmark and Share
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Twitter

tags: , , , , ,
posted in Blog by Amir Harel

Follow comments via the RSS Feed | Leave a comment | Trackback URL

  • gene99
    Amir, I got the code working for a url like www.ebay.com & ebay.com but not http://www.ebay.com . I removed the "http://" from 2 places in the "res_iframe.js but i still won't work. My php echo includes the "http://". What can I do? >> my > <?php echo
    $custom_1; ?> = a full url w/ "http://" Thanks
    this works fine > http://www.easysavannah.com/res_iframe.html?ifr...
    I hardcoded it though
  • I'm no expert in php but it seems like you have some configuration issues there- the code works fine for several people who uses this code.
    Worst case, you can always enforce the removal of the http:// section in the JS side...
  • gene99
    Not to bother you but any idea how I can do that? I entered a url w/ out the http:// in a custom field here > http://www.easysavannah.com/moon-river-test-4.html . Click the second down "See Our Google Reviews & Map" on the middle column to the right of the "business hours". This site is just testing codes). I don't know how to drop http:// w. js. Thanks
  • i think this is a security issue with hostgator (are you hosting there?)
    You can read more here:
    http://stackoverflow.com/questions/1089744/403-...
    http://code.google.com/p/timthumb/issues/detail...

    good luck
  • gene99
    That was the problem! Yes I use HostGator. Took them 30 seconds to make the change talked about here> http://code.google.com/p/timthumb/issues/detail... Thank you so much! I owe ya :)
  • gene99
    Amir I've been trying to solve this for 2 months. Thank you!
  • xperiax1keyboardhater
    Amir, great piece of work. However, I wonder if there is a way to change the URL in the address bar each time a page loads, so that the query string contains the correct iframe URL. This would be great for bookmarking and return visits. At the moment, ?iframe= the default URL contained in the js file.

    Any ideas?
  • xperiax1keyboardhater
    Sorry, you had already answered this in the article...

    "Due to security reasons the browser prevent access from reading an iframe source url, so if the user has clicked a few links in the ifrmae, there is no way to find out which page is being displayed in the iframe."
  • xperiax1keyboardhater
    Although perhaps the solution used in the following link might be worked into your solution for this purpose..?

    http://forums.devarticles.com/programming-tools...
  • xperiax1keyboardhater
    Brilliant piece of coding this. I found this very useful. Not using all of the functionality, as I just wanted to be able to resize an iframe to the content and keep a nav bar at the top of the page (don't want any of the toolbar functionality). I tried many different snippets of code and this was the best, so thanks :-)
  • You're welcome!
    And thanks.
  • Pre
    Question, but what if you are not using this toolbar and you just want to use this script for you own content that needs to be loaded from another page on the site. What do we need to replace with what?
  • Not sure i understood your question.
  • Ross
    Is it just me, or does this not work at all in Internet Explorer (tried in both 7 & 8).
  • Great tutorial, been looking for something like this. The only thing that would keep me from using this (cause I did get it to work) is the url length. I would love to see a way to convert those links into its own short url similar to digg automatically.
    example http://www.amirharel.com/olknjfsdoi
  • Hi Jon, Thanks.

    In order to use short URLs you can either implement such a service on your own or use an existing service.
    If you want to host a short URL service you can check out these open source solutions:
    * http://yourls.org/
    * http://www.sean-o.com/blog/index.php/2009/08/11...

    If you don't want to host your own servce and would like to use existing on, i recommend to checkout bit.ly API: http://code.google.com/p/bitly-api/wiki/ApiDocu...
    This will allow you to create short URLs using an API and to get all the tracking information of bit.ly - which is great.

    and most importantly - Telescreen is awesome listening over and over to "Exit" great stuff.

    Regards,
    Amir
blog comments powered by Disqus
 
Powered by Wordpress and MySQL. Theme by openark.org