David Taylor - Front-End Interface Web Developer

Cross Domain or Protocol iFrame Communication

We had this elephant in the closet issue while working on a project about a year ago. We were serving the website using a regular http connection but switching to a secure https connection. The login and registration process worked beautifully as we began the build supporting the organic non-javascript journey.

dem dialogs

We hit the issue as we began to build the dialogs. We quickly realised that the user would need to log in using a secure page on a dialog from within a non-secure parent page. These two pages would also need to communicate to make the user experience smooth and clear. We researched various ways of talking cross domains but found the browsers we needed to support weren’t consistent enough.

Fortunately as the majority of the site was to be secure anyway we opted to serve the site completely from https but I hated being beaten.

victory

Being a stubborn perfectionist I was determined to find a way to get this to work and I got the opportunity last month.

iFrame Messaging

The key behind this technique is that pages on the same domain and protocol can communicate with each other as long as they have access to each others window. There are various ways this could work but this is how I suggest you start.

1. Parent page

Your parent page should expose a public function which will provide the functionality you want to use.

window.myPublicFunction = function(message){
    console.log(message);
};

Remember if you’re writing any values in to the page you need to escape the value to protect against any potential cross-site scripting vulnerabilities.

2. Script Interface

On the same domain as your parent page you will need a page which simply includes a single script reference.

Watch out for potential cross-site scripting vulnerabilities.

This page will be called for every message you want transmitted across domains/protocols to the top window. You would call it passing it a key and a value query string value.

For example, say you wanted to pass log messages you could call the page like: http://mysite.com/xinterface.html?key=LOG&value=message.

xinterface.html

This is what the html page would look like.

<script src="xinterface.js"></script>

xinterface.js

The script would use a secure regular expression to pull out the values you want from the url. And then call an appropriate function in the top window.

// use a strict regular expression for security
var query = /key=([A-Z]*)(&value=([a-z|A-Z]*))?/.exec(location.search),
    key   = query[1],
    value = escape(query[3]);

// check if the url is valid
if (query && key) {

    // call the public function
    if (key === 'LOG') {
        window.top.myPublicFunction(value);
    }
}

3. The Dialog Page

The final piece of the puzzle is the secure page itself. You will have an iframe positioned so that it is used for the dialog content and it will be showing the secure page (eg: https://mysecuresite.com/dialog.html).

Now whenever you want to communicate you just inject an invisible iframe within the page with the appropriate url.

You could wrap it in a function:

var body = document.getElementsByTagName('body')[0],
    xinterfaceUrl = 'http://mysite.com/xinterface.html';

function communicate(key, value){
    var src = xinterfaceUrl +'?key='+ key +'&value=' + value,
        iframe = document.createElement('iframe');
    iframe.src = src;
    iframe.style.display = 'none';
    body.appendChild(iframe);
}

Feedback

As always, one of the reasons I blog about these things is to validate the thinking so If there is something I’m missing let me know. Hope this is useful.

NB. The Future of Cross Domain or Protocol Communication

As the older browsers die out there are new ways of giving permission for specific domains to communicate between each other but that is for another post.

Aqueduct, a place on the edge

Aqueduct

I have just finished my time at Aqueduct after two and a half years leading the front-end work there.

I leave very satisfied, after feeling like we’ve come a long way in that time and the path ahead for those guys is looking pretty exciting.

This small agency I joined has grown (and grown up) quite significantly over my time there. This has been credit to a number of people. For the dev team ask any of the developers that have gone through the agency over the last few years and they will all tell of how inspirational Guillaume is as a person and how he has built a dev team which is now fueling itself to do great work. He’s a good one for quotes and this was a gem recently:

We go all out for success knowing that we will sometime fail. But what we don’t want is to sit in the middle

I’ve often taken as one of my own mottos “You’ve got to fail to succeed”, in fact in my view if you’re not failing then you’re not taking enough risks and aren’t pushing boundaries. Aqueduct gave me the freedom to take risks and in the Agile way tought me how to fail fast so that you can rectify and learn from your mistakes.

Highlights

Lloyds of London

I was thrown in at the deep-end with not one but 2 projects each of which were larger than I had done before. I learnt so much during the Lloyd’s of London and British Red Cross projects, partly from late night researching, tweaking and fine tuning but also learning from the brilliant teams around me especially from Martin, Arnold and Simon at the time.

FA Just Play

Recently I have also had the privilege to work closely with Tristan on a campaign site for The FA which picked up the first award attributed to one of my projects. This was a great close collaboration on many fronts and I have a lot of respect for designers who are creative with technology and are open to be inspired by us techie people. He was able to mould it in to a great site we are all proud of.

RFU Six Nations

Finally we have recently been involved in some really great work for the RFU. We inherited their current site and have been gradually cleaning up and optimising the site along with doing some fun front-end heavy stuff for the World Cup and currently the Six Nations.

Team

The best thing about working at Aqueduct was the team, I was really fortunate to be able to work closely with both the developers and designers.

Steve

It was also great to be directly involved in recruiting and building the front-end team. Steve joined me on a permanent basis in January 2011 and together we have been able to see the fruits of sticking to standards and continuously pushing our capabilities.

It is a real strength that people from all corners of the agency are empowered to bring value to the agency. And this is encouraged from the very top.

Byez

And so as I take my family on a short adventure over the next five months, I’m sad to leave all that is bubbling in this great agency but look forward to continuing a relationship with them and recommend anyone who want’s to “make cool things” to check them out.

Adam Ball Portfolio and Exploring Responsive Images

I have just finished a project for Adam Ball who is an artist based in London. This has been a real challenge and has made me push boundaries in to new territory.

Responsive Design, Responsive Images

The main area has been with serving appropriate image sizes according to the device. I have previously talked about my responsive images jQuery plugin which is a good stop gap for getting this to work across the current browser landscape. I have liked working with responsive images in this way because it is a much more organic solution than the ones I have seen elsewhere. It’ll be great when we’ve got something natively to handle this sort of thing.