1

I am setting the source of an iframe to an html string as shown below to have it execute a string of html that I have stored in memory.

window.sHTML = html;
iframe.src = 'javascript:parent.sHTML';

The html string includes javascript code like this:

window.onerror = function(a,b,c) {
  console.log(a);
  console.log(b);
  console.log(c)
  return true;
}

When are error occurs in the iframe it logs "Script Error", "", "0" rather than giving me the actual error information.

I understand that this can happen when the iframe in question is cross domain: Cryptic "Script Error." reported in Javascript in Chrome and Firefox

However, the iframe is not cross domain, it is just something I created dynamically. Is there any way to make window.onerror treat it as a non-cross domain iframe so that I can access the proper error information from window.onerror?

Community
  • 1
  • 1
asutherland
  • 2,849
  • 4
  • 32
  • 50
  • have you tried something like `iframe.innerHTML = html;` instead of using the src property? [related](http://softwareas.com/injecting-html-into-an-iframe) – jbabey Sep 06 '12 at 12:46
  • possible duplicate of [load a document string into an iframe](http://stackoverflow.com/questions/1288586/load-a-document-string-into-an-iframe) – jbabey Sep 06 '12 at 12:48
  • Setting the innerHTML doesn't make the iframe execute scripts afaik. I haven't tried the document.write method in answer you mentioned in your second reply, trying that now. Note that the code I listed above DOES work to make the iframe execute an html string so thats not really the probably I am trying to solve and this is not a duplicate question. My question is specific to window.onerror and how to load a document string into an iframe AND ensure that window.onerror still works properly. – asutherland Sep 06 '12 at 13:00
  • is the javascript you posted the only thing in the html string? You could just do `iframe.contentWindow.onerror = function(a,b,c) {...};` from the parent. – jbabey Sep 06 '12 at 13:02
  • Just tried use the document.write method and it works, so I'm all set, thanks! Regarding doing it from the parent, the string is a big JS file, that code is just a tiny part of it, but I still might be able to do iframe.contentWindow.onerror = ... for that as well. Will try that too and post the result here. Result: Doesn't seem to work – asutherland Sep 06 '12 at 13:13
  • Where di you learn about `'javascript:parent.sHTML';`? That looks wrong on multiple levels. – epascarello Sep 06 '12 at 13:16
  • @user959986 i've posted this as an answer. if it helped you or you feel it would help future visitors, feel free to upvote and/or accept. – jbabey Sep 06 '12 at 13:17

1 Answers1

0

If you need to dynamically populate iframe content with an HTML string, document.write would probably work:

var iframe = document.createElement('iframe');

document.body.appendChild(iframe);

iframe.contentDocument.open();
iframe.contentDocument.write(yourHTMLString);
iframe.contentDocument.close();

reference

jbabey
  • 45,965
  • 12
  • 71
  • 94