5

I am trying to do something like:

<a href="http://www.google.com">Google</a>

When a user hovers the link:

<div id="display-site">

//working site contained in a div

</div>

Note I am aware that I can open the link in a new window using html, thought I am interested in figuring out how I would go about 'previewing' the website contained in the <a> tag, in a div container, (if the link is hovered).

FruitBreak
  • 570
  • 1
  • 7
  • 19
AnchovyLegend
  • 10,873
  • 31
  • 123
  • 211
  • If your server supports PHP, this [link](http://stackoverflow.com/questions/1237707/load-website-into-div) maybe useful. – Farahmand Jun 24 '12 at 18:39
  • use the tooltip concept with the iframe inside the div content... EDIT: yep Michael is correct.. – sree Jun 24 '12 at 18:04

4 Answers4

9

This can be done by creating an <iframe> in the DOM on hovering over an <a> and loading the href as the iframe's src= attribute. In order to make it look like a popup, you would need to position the <iframe> at an absolute location, and set its z-index CSS property to a higher value than the rest of the page content.

However, if you need to make modifications to the display of the loaded frame, such as sizing some elements to accommodate the zoom level as suggested by @David's answer, you may run afoul of the same-origin policy, as scripts will not be permitted to access properties of the loaded frame outisde the same domain.

From MDN:

Scripts trying to access a frame's content are subject to the same-origin policy, and cannot access most of the properties in the other window object if it was loaded from a different domain. This also applies to a script inside a frame trying to access its parent window. Cross-domain communication can still be achieved with window.postMessage.

Michael Berkowski
  • 253,311
  • 39
  • 421
  • 371
  • 1
    What's the same origin policy got to do with this? Just opening a website in an iframe is always possible isn'it it? I'd be more afraid of websites that don't like to be opened in frames and open themselves in `_top`. – Rudie Jun 24 '12 at 18:13
  • @Rudie I misread MDN docs and then one of my own tests failed, but you are correct. – Michael Berkowski Jun 24 '12 at 18:22
3

Before continuing - check this benefits the user experience. When I move my mouse over a page and brush over a hyperlink I don't always want a link preview to appear on top. However, assuming this is in the best interests of your users...

Implementation wise, this can be done, as @Michael suggests, by using an <iframe>, however the document within the iframe will be displayed at the user's set zoom level, but showing a 250x250 window of a document designed for at least 1024x768 isn't going to help the user. Thus you need to display a zoomed-out, birds' eye representation of the web-page to the user.

There are ways to get the current viewport zoom level ( How to detect page zoom level in all modern browsers? ) but I don't know how setting it works (in all liklihood it's probably impossible in most cases). Furthermore I don't think you can set zoom on a per-iframe basis (assuming you can set it all).

The best way forward then is to display a scaled-down bitmap page rendering to the user - like Google does for popular pages in its search results. However this means that for every page you link to you need to get a rendered image of the target page.

I remember a few years ago there were companies that provided page thumbnail services (it was part of those annoying doubly-underlined ad text in webpages that was popular around 2005-2008), but they're a rarity now.

I guess you'll have to then set up your own service and host a layout engine (Gecko, WebKit, or Trident) in a way it can generate page thumbnails for you.

All things considered, I don't think it's worth it.

Community
  • 1
  • 1
Dai
  • 110,988
  • 21
  • 188
  • 277
0

Something like this, just an idea

$('a').hover(function()
 {
    $('#display-site').load((this).attr('href'));
    $('#display-site').show();
 });

You will need to set the css property as needed

codingbiz
  • 25,120
  • 8
  • 53
  • 93
0

1- Find a jquery plugin that displays tooltips on element hover.
2- Insert an Iframe of the website that the link refers to inside a div residing in the tooltip container.

menislici
  • 1,129
  • 5
  • 18
  • 35