krux / postscribe

Asynchronously write javascript, even with document.write.
MIT License
986 stars 157 forks source link

Problem with iframe contentWindow and Firefox #126

Closed d3x7r0 closed 9 years ago

d3x7r0 commented 9 years ago

I just received a complain from the firm that handles our advertising for us that some ads weren't loading on Firefox and, drilling down, I found that the issue lies with some new code they are using that uses the iframe.contentWindow to inject code into the page. This seems to work perfectly on Chrome but on Firefox 42.0b7 there's a quick flash of the iframe and then it's quickly deleted.

I've been trying to drill down into what's happening but can't seem to find the root of the problem.

A test case for the issue can be found on jsbin: http://jsbin.com/sogulel/edit?html,js,console,output

I would really appreciate any help tracking this one down.

jeffallen6767 commented 9 years ago

Luis,

I think the problem may be contentWindow instead of contentDocument ?

-jeff allen

From: Luis Nabais notifications@github.com<mailto:notifications@github.com> Reply-To: krux/postscribe reply@reply.github.com<mailto:reply@reply.github.com> Date: Tuesday, October 20, 2015 at 2:02 PM To: krux/postscribe postscribe@noreply.github.com<mailto:postscribe@noreply.github.com> Subject: [postscribe] Problem with iframe contentWindow and Firefox (#126)

iframe.contentWindow

jeffallen6767 commented 9 years ago

LINK:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement

From: Jeff Allen jallen@krux.com<mailto:jallen@krux.com> Date: Thursday, October 22, 2015 at 10:48 AM To: krux/postscribe reply@reply.github.com<mailto:reply@reply.github.com> Subject: Re: [postscribe] Problem with iframe contentWindow and Firefox (#126)

Luis,

I think the problem may be contentWindow instead of contentDocument ?

-jeff allen

From: Luis Nabais notifications@github.com<mailto:notifications@github.com> Reply-To: krux/postscribe reply@reply.github.com<mailto:reply@reply.github.com> Date: Tuesday, October 20, 2015 at 2:02 PM To: krux/postscribe postscribe@noreply.github.com<mailto:postscribe@noreply.github.com> Subject: [postscribe] Problem with iframe contentWindow and Firefox (#126)

iframe.contentWindow

d3x7r0 commented 9 years ago

Same issue: http://jsbin.com/rugewa/3/edit?html,js,console,output

You can clearly see that the document is properly written in both cases but latter the iframe is apparently reset. I'm really not sure if this is a postscribe or a Firefox issue but I'm finding it difficult to track down where in postscribe the change might be happening.

jeffallen6767 commented 9 years ago

Please see if the information here:

http://stackoverflow.com/questions/9967478/iframe-content-disappears-on-firefox

And

http://stackoverflow.com/questions/7828502/cannot-set-document-body-innerhtml-of-iframe-in-firefox

Can help you?

-jeff

From: Luis Nabais notifications@github.com<mailto:notifications@github.com> Reply-To: krux/postscribe reply@reply.github.com<mailto:reply@reply.github.com> Date: Thursday, October 22, 2015 at 2:52 PM To: krux/postscribe postscribe@noreply.github.com<mailto:postscribe@noreply.github.com> Cc: Jeff Allen jallen@krux.com<mailto:jallen@krux.com> Subject: Re: [postscribe] Problem with iframe contentWindow and Firefox (#126)

Same issue: http://jsbin.com/rugewa/3/edit?html,js,console,output

You can clearly see that the document is properly written in both cases but latter the iframe is apparently reset. I'm really not sure if this is a postscribe or a Firefox issue but I'm finding it difficult to track down where in postscribe the change might be happening.

— Reply to this email directly or view it on GitHubhttps://github.com/krux/postscribe/issues/126#issuecomment-150321343.

jeffallen6767 commented 9 years ago

In plain words, I think the problem ( in both examples ) is in the _content added, not the postscribe project.

In both examples, the _content added is attempting to create another iframe and directly insert content into it, the problem is that in firefox we need to let the created iframe completely load before modification ( a firefox issue I think ). The work-around would be to modify the _content to use the following pattern:

  1. Create new iframe element
  2. Add (new iframe).onload event handler
  3. Insert iframe element into document
  4. Insert iframe content in onload event created above

This should allow iframe to complete loading BEFORE adding the content which should help the problem

( does that make sense? ) -jeff

From: Jeff Allen jallen@krux.com<mailto:jallen@krux.com> Date: Thursday, October 22, 2015 at 12:49 PM To: krux/postscribe reply@reply.github.com<mailto:reply@reply.github.com> Subject: Re: [postscribe] Problem with iframe contentWindow and Firefox (#126)

Please see if the information here:

http://stackoverflow.com/questions/9967478/iframe-content-disappears-on-firefox

And

http://stackoverflow.com/questions/7828502/cannot-set-document-body-innerhtml-of-iframe-in-firefox

Can help you?

-jeff

From: Luis Nabais notifications@github.com<mailto:notifications@github.com> Reply-To: krux/postscribe reply@reply.github.com<mailto:reply@reply.github.com> Date: Thursday, October 22, 2015 at 2:52 PM To: krux/postscribe postscribe@noreply.github.com<mailto:postscribe@noreply.github.com> Cc: Jeff Allen jallen@krux.com<mailto:jallen@krux.com> Subject: Re: [postscribe] Problem with iframe contentWindow and Firefox (#126)

Same issue: http://jsbin.com/rugewa/3/edit?html,js,console,output

You can clearly see that the document is properly written in both cases but latter the iframe is apparently reset. I'm really not sure if this is a postscribe or a Firefox issue but I'm finding it difficult to track down where in postscribe the change might be happening.

— Reply to this email directly or view it on GitHubhttps://github.com/krux/postscribe/issues/126#issuecomment-150321343.

d3x7r0 commented 9 years ago

Thank you very much for your help. I could have sworn I had tried to use the onload event before and it hadn't worked but maybe my lack of sleep betrayed me at the time. Your suggestion worked like a charm, now I just have to get them to actually use it.

I'll go ahead and close the issue now but the fixed code is in this jsbin for anyone that might have the same issue in the future.