views:

213

answers:

2

When building a social bookmarking button widget for usage in other websites there are a few challenges. We just recently opensourced the clientside aspects of this. (blog post here: http://www.mellowmorning.com/2010/08/03/creating-your-own-diggfacebook-liketweetmeme-button/)

Basically the goal is to replace the chosen elements love it With a button showing how many other people loved it.

There are two approaches to this. - replace the A with html (script approach) - replace the A with an iframe (iframe approach)

There are rather complicated differences between these approaches. One particularly annoying is the inability for the script approach to communicate with its popup.

Can anyone suggest a workaround to communicate between the login popup and the button. (IE. when you love something through the popup?, how do you update the count on the button, without being stopped by same origin protection..)

Which approach do you recommend. Iframe or Script and why?

These are the differences I encountered: Iframe vs Script

Iframe:

  • Popup communication possible The script approach cannot communicate with popups it creates due to the same origin restrictions. The iframe however can be of the same domain as the popup and freely communicate. This gives a better user experience when for instance logging in.
  • Easier to develop The iframe approach is easier to develop and requires less code.
  • Parallel download in IE IE doesn’t download the count scripts in parallel, but it does do so for the IFRAMEs. Making this approach somewhat faster.
  • Independent CSS External sites don’t interfere with your button’s css if you use an iframe technique. The disadvantage is that it makes things likes hovers impossible to integrate with the other site. (For example Fashiolista’s compact button).
  • Independent The iframe approach makes it very hard for other sites to game the users like/love action. With a script approach a foreign site can simply call your javascript to fake someone loving the product. This freedom can be abused but also allows for mashups.

Script:

  • Slower dom load Creating iframes takes a lot more time for the browser.
  • Slower perceived load The script approach allows you to format the buttons before the data is loaded. Vastly increasing the perceived load speed.
  • No shared functionality Buttons can’t share functionality. So when someone logs in for one button its is not possible to update the others.
+1  A: 

There is of course a third option too, which is a hybrid between the iframe and the script approach.

You can use script to hook into the pages DOM (gives a loot of freedom with regards to different uses), and to create a hidden iframe pointing to your domain.

The script could communicate both with the current document, and with yours using Cross Domain Messaging and in turn with the popups using the iframe as a proxy.

Of course, the XDM does impose some difficulties, but if you use a proved solution like easyXDM, then it shouldn't be much of a problem.

Here is an example that shows how to interact with a popup.

Sean Kinsey
@Sean Kinsey: the answer written by Thierry is a response to your question. He's the colleague of mine that actually wrote the code (he just didn't have an SO account before).
WoLpH
+1  A: 

Basically 2 questions - Which is the best approach - Workaround for the popup communication difficulties the script approach faces

Thanks for the popup information! What is the underlying technology to support these cross domain popups?

If i understand your third option correctly: - foreign site loading our js - js replacing dom elements - js opening hidden iframe to own domain

How do I then open a popup which still allows for communication with our js loaded into the foreign site? For that the popup would need to be instantiated by the iframe right? And we would need a method to communicate with the iframe. I thought im not allowed to do anything with the iframe except setting its window.location.href. Could you explain how that works?

Thierry
easyXDM is the library that provides the XDM, relaying messages between a page and a popup is only one of the use cases for it.The popup can be opened by either page, but the safest, to avoid blockers is to do it on a user action, and so you will probably have to do it on the 'consumer'. After that it's only a matter of hooking the popup up to the hidden frame and you've got it going.Take a look at this example here: http://consumer.easyxdm.net/dev/example/, it's a dev version of easyXDM that makes this easy. Also see http://github.com/oyvindkinsey/Api which I'm working on now.
Sean Kinsey
Actually, I seem to have broken that dev example :)
Sean Kinsey
And now it's fixed again
Sean Kinsey
Hey Sean, both the current and dev version. http://consumer.easyxdm.net/dev/example/ and http://consumer.easyxdm.net/current/example/ are currently not working for me (chrome). Also which of the transport classes would enable me to communicate with the popup via the iframe? If you have the 3 elements, script in page, iframe in page and popup. The popup cannot reach the page or iframe right. And the page can only set iframe.location.href. (At least as far is I know.). Which technique do you use to enable this? (Or transport class in the easyxdm code. ) BTW easyxdm code looks very clean!
Thierry
Check out my new project, http://github.com/oyvindkinsey/JsApiToolkit (see a rudimentary example here http://kinsey.no/projects/jsapitoolkit/src/test/) for easy integration of all these components. The example shown does not use a popup, but if you flip the switch useModal into false, then it will, and it will work. This project is a framework for building Api's, so it should give you some clues.
Sean Kinsey