0

I have a page with frames. In one frame a user's action can cause an ajax request to be sent. The response contains a complete new page and I want to replace the existing page with the response. My problem is that when I try and do that the new content is rendered within the frame the request originated from. How can I replace the entire page and not just the frame?

I've tried:

document.open();
document.write(response);
document.close();

and:

var newDoc = document.open("text/html", "replace");
newDoc.write(response);
newDoc.close();

But I got the same undesirable results from both.

Larry Martell
  • 3,526
  • 6
  • 40
  • 76

1 Answers1

3

I can't check right now, but assuming:

  • Your Ajax call happens inside a frame one level below the main document
  • Your response contains only the body content

You may try the following code:

document.parent.body.innerHTML = response;
OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • I am getting: document.parent is undefined. My page looks like this: Main The request originates from the selection frame. – Larry Martell Jan 08 '14 at 23:17
  • This post may give you some insight, Larry: http://stackoverflow.com/questions/4733542/reading-parent-document-from-iframe-and-changing-parent – OnoSendai Jan 08 '14 at 23:22
  • I tried both top.document.body.innerHTML = response; and parent.document.body.innerHTML = response; In both case the entire doc was replaced, but only the top frame was rendered. If I look at the page source everything is there, and if I just hit return in the address bar all the frames are rendered. What would prevent it from rendering all the frames when I do the assignment? – Larry Martell Jan 08 '14 at 23:32
  • 1
    I got this to work by doing this: top.document.childNodes[0].innerHTML = response; This works on FF, Chrome, and Safari, but on IE it fails with: SCRIPT600: Could not set the innerHTML property. Invalid target element for this operation. – Larry Martell Jan 09 '14 at 18:42