SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript DOM & Frames... adding div tag problems

    Can anyone help me... I have a website with two frames, and the page provided the frames has javascript that needs to add a div element containing an image into the first frame.

    I'm no DOM expert, but can anyone help me out? Here's what I have so far... Unfortunately the javascript has to be part of the page that is creating the frames and not from within one of the framed pages itself.

    Code JavaScript:
    var snDiv = window.document.createElement('div');
       	snDiv.setAttribute('id', snID);
        snDiv.style.position = "absolute";
    	snDiv.style.left = var1;
       	snDiv.style.top = var2;
       	snDiv.style.backgroundColor = "#ff0000";
       	snDiv.style.border = "1px solid #000000";
    	snDiv.innerHTML = "<img src='admin/blah.gif' border='0'>";
       window.document.body.appendChild(snDiv);

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    This information about dom window frames will help.

    http://developer.mozilla.org/en/docs/DOM:window.frames

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can access a frame or iframe using the following syntax
    Code:
    var frame = window.frames["name_of_frame"];
    You can easily add elements to the frames DOM using the frame reference above:
    Code:
    var p = frame.document.createElement("p");
    p.innerHTML = "tada!";
    
    frame.document.body.appendChild(p);
    Note above that the node was created using the frame reference, since you can not append a node that was created in one document to another document.

    Firefox will throw the following error if you omit the bold part above:
    Code:
    Error: uncaught exception: [Exception... "Node cannot be used in a document other than the one in which it was created"  code: "4" nsresult: "0x80530004 (NS_ERROR_DOM_WRONG_DOCUMENT_ERR)"]
    One last thing, you need to make sure that the frame content is loaded before you start adding elements to it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •