rahul888
rahul888

Reputation: 413

Convert a string into HTML Document

Suppose I regex a string in Javascript and save it in a var.

So my string would be something like :

var reg= "<html><body>..... </body></html>"

or only

var reg= "<body>....</body>

How do I make this string into HTML document that I would be able to display somewhere from a Javascript function.

Upvotes: 0

Views: 190

Answers (2)

Paulo Roberto Rosa
Paulo Roberto Rosa

Reputation: 3295

You can use javascript document.write(). You can use this command to output string that can be a html element or even a javascript code, or css etc.

Sure that you can try things like that:

document.write("<body><div>a</div></body>");

Then you transform your whole page into that.

Or even can you write in a iFrame or a Window, Dialog...

newWindow = window.open("");
newWindow.document.write("<body><div>a</div></body>");

...

newDialog = window.openDialog("");
newDialog.document.write("<body><div>a</div></body>");

I tried with the iFrame but i didnt found the write function into it, unhappily. But you can create element using var div = document.createElement("div") for example and after that you can .appendChild(div) in the iFrame easily this way:

var iframe = document.createElement("iframe");
iframe.id = 'iFrame';
var stringElement = "div";
var div = document.createElement(stringElement);
iframe.appendChild(div);
document.body.appendChild(iframe);

You have too window.showModalDialog() but i dont know how it works kk.

Upvotes: 0

Barmar
Barmar

Reputation: 782693

var win = window.open('');
win.document.write(reg);

Upvotes: 2

Related Questions