views:

1087

answers:

2

When using javascript:parent.location.href in an IFrame to change the parent window, IE briefly changes the IFrame content to list the url that the parent is changing to. Firefox, chrome, and opera just change the parent window.

Is there a way to make IE skip showing the url in the IFrame and just change the parent?

[edit]

here is the sample code to duplicate it.

Iframe page:

<iframe id="finder" name="finder" src="content.html" scrolling="no" height="620" width="620" style="border:1px #c0c0c0 solid;"></iframe>

Content page:

<a href='javascript:parent.location.href="http://www.google.com"'&gt;test link</a>
+2  A: 

This only seems to occur when the JavaScript is inline, as in your example:

<a href='javascript:parent.location.href="http://www.google.com"'&gt;test link</a>

If I move it to an explicit function, I no longer see the behavior:

function goParent(url)
{
    parent.location.href = url;
}

<a href="javascript:goParent('http://www.google.com');"&gt;test link</a>

But of course, there's no reason to use JS for this specific case:

<a href="http://www.google.com/" target="_parent">test link</a>

And if there were, we should still degrade gracefully:

<a href="http://www.google.com/" target="_parent">test link</a>

<script type="text/javascript">
    var links = document.getElementsByTagName('a');
    for(var i=0;i<links.length;i++) {
        if(links[i].target == '_parent') {
            links[i].onclick = handleParentClick;
        }
    }

    function handleParentClick(e) {
        var sender = e ? (e.target || e) : window.event.srcElement;
        parent.location.href = sender.href;
        return false;
    }
</script>
Rex M
+4  A: 

You can leave the javascript inline if you change it to this:

<a href='javascript:void(parent.location.href="http://www.google.com")'&gt;test link</a>

Normally the "set" expression also returns the right hand side of the expression. IE takes the return value of the expression and displays it. By putting "void()" around it, you remove the return value so IE doesn't display it.

David
This describes the problem most accurately — “javascript:anything-non-null” displays the result as a string. Rex's advice with the target attribute is the practical way forward though. Top tip: never ever use javascript: URLs for anything, ever.
bobince
+1 This should be the correct answer, the solution by Rex M does not show that they understand the real problem. However, if you are going to use javascript, there's no reason to use the javascript: in the href attribute, just set its onclick handler, even better, do it non-intrusively.
Juan Mendes