0

I am trying to inject code into an iframe but I haven't had any luck and I'm not sure why. I store my js code into a variable, which works properly at this point, into a variable called pixel_code. I then want to inject that code into the iframe. Assuming the id of the iframe is "iframe_" how would I inject the code (variable is pixel_code) into the iframe and execute it? I think jquery would be the easiest way, but I'm open to regular JS as well.

One of my many unsuccessful attempts is:

function captureValue(){

pixel_code = document.getElementById("baseText").value;

$('#iframe_').find('body').append(pixel_code);

}

No errors are generated in the console.

  • Is the iframe and the parent page in the same domain? –  Jan 25 '13 at 01:58
  • I am running on a localhost, so I assumed the answer would be yes. Is that incorrect? Here's the error I get http://cl.ly/image/3k1s0q212a0a – Victor Torres Jan 25 '13 at 02:10
  • Appears that you are not using the http:// protocol. May you paste the HTML part that you call the iframe? –  Jan 25 '13 at 02:50

1 Answers1

3

Try using contents method:

$('#iframe_').contents().find('body').append(pixel_code);

Note that domain of the iframe should match with the domain of the parent page, otherwise Same Origin Policy prevents accessing the iframe's contents.

Ram
  • 143,282
  • 16
  • 168
  • 197