I need to edit an HTML document using jQuery, but all solutions I have tried are very restrictive.
I am fetching the document from the server, so I get it as a string. In order to parse the document, I need to add it in the DOM.
When I append it to the body, I lose <html>
, <head>
and <body>
tags, which are stripped by the browser (see https://stackoverflow.com/a/3218941/654179).
So I tried using an iframe, which is a bit complex: I have to use contentWindow.document
to fill it manually, then a trick with contents().find('*').html()
to get the content. Maybe there is a simpler, cleaner way of doing this. But the iframe solution also strips <html>
, <head>
and <body>
tags. See my example: http://jsfiddle.net/gNvYn/7/
There is another trick here: https://stackoverflow.com/a/6751201/654179 but I couldn't use it for writing then reading the document (I need to read it for saving on the server).
So, is there a way to edit (write, then read) an HTML document without losing the structure?