Check target container is attached to ownerDocument

Not global document as that will cause problems with iFrames.

Change-Id: I0e7afa563b1180270c4119bd9a9c2fd5272b031d