To get the value of IFRAME and its elements

Discussion in 'PHP' started by mandarchalke29, Dec 7, 2007.

  1. #1
    I have created an IFRAME in my page and i wanted to get the value of the elements placed on it. i have coded it like this.

    <body onload="javascript:chat_init();" bgcolor="#EBF3FB">
    <iframe id="chatContents" name="chatContents" src="contents.html"></iframe><br>
    <script type="text/javascript">
    var cWindow;
    var cDocument;
    var id11;

    function chat_init()
    {
    var chatContents = document.getElementById("chatContents");
    if(chatContents.contentWindow)
    cWindow = chatContents.contentWindow;
    if(cWindow.document)
    cDocument=cWindow.document;
    }

    function insertMessages(content)
    {
    //place the new messages in a div
    var newDiv = cDocument.createElement("DIV");
    newDiv.innerHTML = content;

    //append the messages to the contents
    cDocument.getElementById("contents").appendChild(newDiv);

    //scroll the chatContents area to the bottom
    cWindow.scrollTo(0,cDocument.getElementById ("contents").offsetHeight);
    }


    But everytime when i load the window it says cDocument is null as a javascript error and if i refresh it, it works fine. Why does so. Will anyone please help me?

    Mandar :D
     
    mandarchalke29, Dec 7, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    I guess is because when chat_init() is executed your window has been loaded but your iframe has not been loaded yet. I'm not sute if is possibl, but you can try with "onload" event for your iframe.
    Anyway, you should put this question on Javascript subforum, not on PHP subforum.
     
    ajsa52, Dec 7, 2007 IP