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=""'>test link</a>
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."'>test link</a>
Share
Improve this question
edited Mar 11, 2009 at 19:39
Mike
asked Mar 11, 2009 at 18:16
MikeMike
1642 silver badges7 bronze badges
3
- IE7 doesn't seem to do this. Can you post some code? – Chris Van Opstal Commented Mar 11, 2009 at 18:25
- Where is it showing the URL exactly? – Assaf Lavie Commented Mar 11, 2009 at 18:27
- The content of the IFrame is being replaced with text showing the URL that the parent eventually changes to. – Mike Commented Mar 11, 2009 at 19:32
2 Answers
Reset to default 7This occurs because you are evaluating an expression, rather than invoking a method.
parent.location.href = "http://www.google.";
If we move it to an explicit function, we no longer see the behavior:
<a href="javascript:void(parent.location.href = 'http://www.google.')">test link</a>
But of course, there's no reason to use JS for this specific case:
<a href="http://www.google./" target="_parent">test link</a>
And if there were, we should still degrade gracefully:
<a href="http://www.google./" 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>
You can leave the javascript inline if you change it to this:
<a href='javascript:void(parent.location.href="http://www.google.")'>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.