Nir
Nir

Reputation: 25359

IE - entire page redirects if iframe src is set with bad URL

The code below shows the problem. It creates a simple iframe.

On browsers besides IE if the url is set to http:[4 slashes] test.com instead of http:[two slashes] test.com the iframe will show an error. on IE the parent page redirects to an error page.

I'm using an iframe in a widget in other people's website and this can cause a lot of damage in case of an error. My question is how can it be avoided so if the SRC url is bad the error will remain in the iframe and not cause the entire page to redirect.

To check you can copy the code to jsfiddle.net and run.

popup = window.document.createElement('div');
popup_html="<div style='width:300px;height:300px; ' >";
popup_html+=" <iframe src='http:////www.test.com' width='300' height='300' frameborder='0' ></iframe>";
popup_html+=" </div>";
popup.innerHTML=popup_html;
//cbar_popup.style.visibility='visible';
window.document.body.appendChild(popup);

Upvotes: 1

Views: 1651

Answers (2)

karthick
karthick

Reputation: 12176

To prevent the redirect you can try this.

popup = window.document.createElement('div');
popup_html="<div style='width:300px;height:300px; ' >";
popup_html+=" <iframe id='myframe' src='' width='300' height='300' frameborder='0' ></iframe>";
popup_html+=" </div>";
popup.innerHTML=popup_html;
//cbar_popup.style.visibility='visible';
window.document.body.appendChild(popup);
document.getElementById('myframe').src="http:////www.test.com";

I don't know the actual reason why it's happening. I am analyzing the js that is called by ieframe.dll. If I found anything in regards to this I will update my answer.

Upvotes: 0

louisbros
louisbros

Reputation: 875

You could try something like this to clean the url and only attempt the popup stuff if the url is valid:

var url = 'http:////stackoverflow.com/questions/16076720/ie-entire-page-redirects-if-iframe-src-is-set-with-bad-url';
var result = /^(?:(?:http[s]?:\/{2,4})?(?:www\.)?)?([\w-_]+)\.([\w\.]+)([\/\?\w-_\=\"]*)/.exec(url);

if(result){
    url = 'http://'+result[1]+'.'+result[2]+result[3];
    console.log(url);
    // do the popup stuff here with cleaned up url
}

Upvotes: 1

Related Questions