1

currently i am using bellow code

<html>
  <head>
    <script>
      window.onload = function(){
      var parser = new DOMParser();
      var html = "<html><body><p>this is" + 
          "<script>document.write('dynamic')<\/script> text.</p></body></html>";
      var doc = parser.parseFromString(html,'text/html');
      document.body.appendChild(doc.body.children[0]);
      }
    </script>
  </head>
    <body>
        <p>this is <script>document.write('dynamic')</script> text.</p>
    </body>
</html>

that shows : this is dynamic text.

but currently i am loading page dynamicaly by ajax and parsing using new DOMParser. but it not prints "dynamic" only shows: this is text.

2
  • That's how dw is designed to work. You can't use it to add content to a page after the page has been parsed, unless you don't want to re-write the whole document, including the head section. Use proper DOM manipulation methods instead. Commented Oct 24, 2016 at 13:47
  • Don't use document.write at all. Commented Oct 24, 2016 at 13:51

1 Answer 1

1

You can use the DOM manipulation methods shown here:

var h = document.createElement("H1")                // Create a <h1> element
var t = document.createTextNode("Hello World");     // Create a text node
h.appendChild(t);  

or here: document.getElementById("demo").innerHTML = "Paragraph changed!";

As you are currently loading the page via AJAX, you probably have to make sure that everything flows in the right order, i.e.: the change of the DOM is done after the page is received, etc. (Don't know your surrounding code, so I can't really speak to that).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.