Mike
Mike

Reputation: 164

How to stop IE from displaying URL in IFrame when changing the parent window

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"'>test link</a>

Upvotes: 2

Views: 2304

Answers (2)

Rex M
Rex M

Reputation: 144112

This occurs because you are evaluating an expression, rather than invoking a method.

parent.location.href = "http://www.google.com";

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

<a href="javascript:void(parent.location.href = 'http://www.google.com')">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>

Upvotes: 7

David
David

Reputation: 34543

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

<a href='javascript:void(parent.location.href="http://www.google.com")'>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.

Upvotes: 4

Related Questions