1

I think what I've got might not require much adjusting, but when I try the following:

<div id="exp_div"></div>
<script>
procedure = [
    [
      "content 1","content 2"
      ]
    ]

procedure.forEach(function(this_proc,index){
  $("#exp_div").append("<iframe id='trial"+index+"' width='800px' height='800px'></iframe>");
  trial_iframe_code = '';
  for(var i = 0; i < this_proc.length; i++){
     trial_iframe_code += "<iframe id='post"+i+"'></iframe>";
  }
  doc = document.getElementById('trial'+index).contentWindow.document;
  doc.open();
  doc.write(trial_iframe_code);
  doc.close();
  for(var i = 0; i < this_proc.length; i++){
    trial_content = this_proc[i];
      console.dir(trial_content);
    doc = document.getElementById('trial'+index).contentWindow.document.getElementById('post'+i);
    doc.open();
    doc.write(trial_content);
    doc.close();
  }
})
</script>

i.e. at this fiddle (iframes don't work as well when i try to run the code on SO) I get the error that "doc.open is not a function" - which I think relates to the attempt to write on the "post" iframe within the trial iframe. Is it possible to write to an iframe within an iframe in this sort of structure? Is my code close to correct, or is a different approach required?

To clarify, this is for an online psychology experiment to create all the tasks the participant will do in a series of iframes. As some of the tasks will have multiple tasks within them, I want to write multiple iframes within an iframe when this happens.

Working code in this case would produce an iframe called "trial0" within "exp_div". Within "trial0" there would be an iframe with id "post0" which just has "content 1" written in it, and an iframe with id "post1" which has "content 2" written in it.

  • There are two ways in which you can set content to an iframe one is setting the content url to the src attribute or by creating object URLs in case you get the content as byte stream. Object URL way does not really work with IE but you can try that out. – Bose_geek May 15 '18 at 12:05
  • Sorry if I'm misunderstanding, but aren't the two ways you're talking about for when the content is on another web page? In my case all I want to do is put "content 1" in an iframe called "post0", and "content2" in an iframe called "post1" - and these iframes in an iframe called "trial0". So in my case I don't have external content. I've found you can write dynamically into an iframe directly (which is how I created the two iframes within trial0), but it's in the case of an iframe within an iframe that i'm struggling with. – beepingbopping May 15 '18 at 12:08
  • It's not possible to write inside an Iframe, but you could use divs instead of iframes. Or put code in other file which is called from iframe url. – Emeeus May 15 '18 at 13:23
  • @Emeeus I thought it was possible, e.g. https://stackoverflow.com/questions/4199458/specifying-content-of-an-iframe-instead-of-the-src-to-a-page?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa - I feel like I'm missing something as Bose_geek and you both are saying it's not possible. – beepingbopping May 15 '18 at 13:27
  • oh wait - just got it - it's not possible to write content to the "post" iframes because that would be writing over content in the "trial" iframe after it's already been created- right? – beepingbopping May 15 '18 at 13:30
  • @Emeeus - I'm afraid in this instance I need javascript variables between the iframes to not interfere with each other, so I think a div based structure would not contain the variables in the same way an iframe does. – beepingbopping May 15 '18 at 13:33
  • So - I'm an idiot - I just saw a typo that was breaking the code rather than it being difficult conceptually - I failed to add .contentWindow.document to the end of the line : doc = document.getElementById('trial'+index).contentWindow.document.getElementById('post'+i) **.contentWindow.document** ; - I guess I should delete this question then? Sorry for wasting the time of the people commenting. – beepingbopping May 15 '18 at 13:42
  • @itHaffens I'm sorry I misunderstand your question. You could make variables without interference with classes somegroup.someparam1 = "something"; then somegroup2.someparam1 = "something"; – Emeeus May 15 '18 at 13:47
  • @Emeeus thanks for the suggestion - I might look into doing that way. I've managed to make the iframes solution work, but if I decide to go with your div solution that structure could be helpful. – beepingbopping May 15 '18 at 13:49

0 Answers0