Link to home
Start Free TrialLog in
Avatar of jayh99
jayh99

asked on

Javascript from IFrame write to DIV in page

Can someone help me writing from an IFRAME to the div in my parent page.  I am trying the code below, but I get a js error when it runs.  I am guessing that if it is possible, the error is in the document.getElementById(id);.  The script is in my IFRAME, and the DIV is in my parent page.  Thanks in advanced for the help.  



function writit(text,id)
{
      if (document.getElementById)
      {
            x = document.getElementById(id);
            x.innerHTML = '';
            x.innerHTML = text;
      }
      else if (document.all)
      {
            x = document.all[id];
            x.innerHTML = text;
      }
      else if (document.layers)
      {
            x = document.layers[id];
            text2 = '<P CLASS="testclass">' + text + '</P>';
            x.document.open();
            x.document.write(text2);
            x.document.close();
      }
}
Avatar of Cem Türk
Cem Türk
Flag of Türkiye image

function writit(text,id)
{
     if (document.getElementById)
     {
          x = parent.document.getElementById(id);
          x.innerHTML = '';
          x.innerHTML = text;
     }
     else if (document.all)
     {
          x = parent.document.all[id];
          x.innerHTML = text;
     }
     else if (document.layers)
     {
          x = parent.document.layers[id];
          text2 = '<P CLASS="testclass">' + text + '</P>';
          x.document.open();
          x.document.write(text2);
          x.document.close();
     }
}
Avatar of jayh99
jayh99

ASKER

Thanks for the quick response, I tried parent.getElementById, but didn't try parent.document.  I'll give it a try tomorrow morning and let you know if it works.  Thanks again.
Avatar of jayh99

ASKER

I get the following error:

'x' is null or not an object

Avatar of jayh99

ASKER

I just noticed that when I go put
window.parent.name, it gives me a layer within my iFrame.  Is there a way to do like a top.getElementById or a way to find the outermost layer?
Avatar of jayh99

ASKER

*sorry about the broken english in that last response, it was a series of copy/pastes gone wrong.  I meant:

alert(window.parent.name);

gives me a layer within the iFrame.
you should try top.getElementById then.
Avatar of jayh99

ASKER

No luck with the top.getElementById

I was wrong when I wrote:

//
alert(window.parent.name);
gives me a layer within the iFrame.
//

That is actually the name of my iFrame.  I have tried the following code, but it didn't work either.

x = window.parent.parent.getElementById('Results');
alert(x.innerHTML);

Avatar of jayh99

ASKER

x = window.parent.parent.document.getElementById('Results');
Gives - Access is denied error

Does that have anything to do with the embedded frame being on a different web server than the parent?
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jayh99

ASKER

That is what I was afraid of.  It looks like I may have to transfer my pages to the other web server to get it to work that way.  That does make sense from a security standpoint.  Thanks for the help!
Jayh99,

It seems like my comment above answered your question.  The answer was you can't do it because the pages are on a different server.  I'm a little surprised you said you didn't get an answer when your last comment seems to acknowledge it.  I did provide information in that comment on why it wouldn't work but let me know if you still have a question.  Let me know your thoughts on this.

bol
@Netminder - How do we create links to a specific comment using just the new site (sans QP)?  Just curious if you have a tip. :)

bol
@Netminder - Thanks!

bol