Write a new html with js or jquery

2

I wanted to know if there is a way to present a new html page using a string containing all the content of the new page I am trying to present, the code is below:

$("#form").on('valid.fndtn.abide', function () {
        var data = $(this).serializeArray();
        sendAjax('index.php?uc=eventos&a=logView', 'POST', data, function (data) {
            try {
                var data = jQuery.parseJSON(data);
                if (!data.status) {
                    alertify.error("A hora ou a Data inserida excede a hora e a data corrente! " +
                            "Você não pode ver resgistros do futuro!");
                }
            } catch (e) {
                document.open();
                document.write(data);
                document.close();
            }
        });
    });

This document.write(data); is where I write a new HTML page with the string received from the server, in firefox it works at first but if I update the resulting page it breaks all the formatting and Chrome repeats elements, is not a viable alternative, my question is, within that "date" I have my new html in string as I do to display the same in the browser window without using document.write.

Thanks in advance for your attention and help.

    
asked by anonymous 30.09.2015 / 15:59

2 answers

0

Friend, I'd like to first give you a little advice, try not to use try-catch to control the flow of the application, after all try-catch is to handle errors, nothing more.

As for document.write , it's best to create a DocumentFragment with to DOMString and add it to body .

var form = document.getElementById("form");
form.addEventListener("valid.fndtn.abide", function (event) {
  var formData = new FormData(form);
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("POST", "index.php?uc=eventos&a=logView");
  xmlHttp.onreadystatechange = function (event) {  
    if (xmlHttp.readyState == 4) {  
      if (xmlhttp.status == 200) {  
        var template = document.createElement("template");
        template.innerHTML = xmlhttp.responseText;
        document.body.appendChild(template.content);
      } else {  
        alertify.error(xmlhttp.statusText);  
      }  
    }
  }
  xmlHttp.send(formData);
});
    
30.09.2015 / 16:25
0

Yes, it is possible.

With Js Pure:

document.getElementById("ID_DIV").innerHTML = "<input type='text'> </input>"; 

And with JQuery:

$("#ID_DIV").html("<input type='text'> </input>");
    
30.09.2015 / 16:02