replace doc.write with doc.appendChild