How to select and replace the whole page with jQuery

前端 未结 5 2084
Happy的楠姐
Happy的楠姐 2020-12-29 13:01

My design of a page forces me to refresh the whole page with html that I have loaded via ajax.

$(\'html\').replaceWith(data);

Gives me errors. A

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 13:33

    this might be the solution you'll require, It replaces the entire document including its head. You won't have any issues loading your new css, js & other resources. I've assumed you receive the new html content by invoking a REST api:

    $.ajax({
      type: 'POST',
      url: form.attr('action'),
      data: form.serialize(), // serializes form elements
      success: function(response) {
        // re-writes the entire document
        var newDoc = document.open("text/html", "replace");
        newDoc.write(response);
        newDoc.close();
      }
    });
    

提交回复
热议问题