views:

863

answers:

2

Here is the situation:

  • A page (iframe.html) has an iframe loading another page (iframe-content.html).
  • An JavaScript error might happen when iframe-content.html is loaded in the iframe.
  • I'd like that exception to be visible to the browser (e.g. shown in Firefox error console, or Firebug).

Here is what I see:

  1. When iframe.html is initially loaded, and loads iframe-content.html with src="iframe-content.html", the JavaScript exception shows in Firebug.
  2. However, if the page is loaded in JavaScript (document.getElementById('iframe').src = 'iframe-content.html'), the exception doesn't show.

You can reproduce this by going to:

  1. http://avernet.googlepages.com/iframe.html with Firefox.
  2. You'll see the exception as iframe-content.html is loaded.
  3. Click on the button: the content of the iframe is loaded again, but this time the exception doesn't show in Firebug.

Is there a way at #3 to have the exception show, instead of it being silently ignored? (You can't use a try/catch around the JS code that sets the src, as this code returns immediately before the page is loaded in the iframe.)

+2  A: 

It seems that your iframe page is not really loaded on the second time. Or it's loaded from the cache and the error is ignored. This is interesting, but I think I found an way around it.

function setContent() {
    try {
        console.log("Loading iframe content");
        document.getElementById('iframe').src = 'iframe-content.html?foo=bar';
    } catch (e) {
        console.log("Caught", e);
    }
    console.log("Done loading");
}

With that the error should appear.

What I did, was to trick the browser to think that I'm loading a brand new page as the parameters after the url have changed.

'iframe-content.html?foo=bar';

You could replace my "bar" string with a changing timestamp. Sure, it would avoid the cache, but it would also force it to generate the error like you wished.

Mikko Tapionlinna
True! The page isn't reloaded. There you go. Thanks Mikko.
Alessandro Vernet
A: 

I have noticed that when i use noscript the iframe get blocked (it can be unblocked). Is it possible for an iframe not to be blocked? This isnt for 'naughty' reasons. Just made a site and the customer wasnt happy that their machine didnt display it (due to 'noscript' being installed).

Thanks in advance.

lien