History.back() in iframe

I’m having a nightmare trying to code a simple back button in an iframe. A Fancybox iframe to be exact. Imagine a site which can open up links in a Fancybox iframe as a preview. The user could open a link and keep clicking inside the iframe. I want them to also be able to click Back inside the iframe.

Also, I don’t want to use an onclick handler, I just want a solution something like this:


function go_back() {
    if( this_is_an_iframe === true ) {
        history.back();
    } else {
        window.history.back();
    }
}

If I use window.history.back() for everything then it works ok on some browsers, but not on others. Some browsers (including mobiles) will just go back the entire page (not the iframe). I’m sure there’s a solution to serve slightly different code when in an iframe.