Jeff Norman
Jeff Norman

Reputation: 1044

How to preview a html file in another tab/window without saving it on disk?

Hy,

In my application I store a string as content of a html file.

How can I preview this content (assuming that it's modified from original content) in browser but not having to save it to disk locally.

And the preview to be in another tab or window.

Does anyone have any idea?

Thanks in advance.

Jeff

Upvotes: 0

Views: 2575

Answers (6)

Michael Schwartz
Michael Schwartz

Reputation: 8415

Here's a Javascript/JQuery solution:

<!DOCTYPE html>
<html>
<head>
<title>Open Custom HTML Page Without Saving</title>
<meta charset='utf-8'>
<meta name='viewport' content='initial-scale=1.0'>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
  $('.preview-site').on('click', function(){
    window.open('javascript:document.write("'+ $('.workflow').val() +'")', 'Opened Page', 'width=660, height=440');
    return false;
  });
});
</script>
</head>
<body>
<a class="preview-site" title="Preview your workflow" href="javascript:void(0)">Preview your workflow</a><br/>

  <textarea class="workflow"><!DOCTYPE html>
<html>
<head>
<title>Hello world!</title>
<meta charset='utf-8'>
<meta name='viewport' content='initial-scale=1.0'>
</head>
<body>
  <h1>Hello World!</h1>
</body>
</html></textarea>
</body>
</html>

Upvotes: 0

Jamie Treworgy
Jamie Treworgy

Reputation: 24344

Is the question mostly just how to open a new window? This depends on what prompts the action. If it's a hyperlink, just add target="_blank". If not or you want more control over the window then use javascript.

Showing content without saving it to disk is the very nature of server-side code, you can't have a web site without doing this.

Response.Write(myString)

Upvotes: 1

StefanE
StefanE

Reputation: 7630

Save the content to a Session variable and make a dummy page and load to content from the Session variable in the Page Load event.

Where your temp content are created

Session["TempPage"] = Content;

And then have a ShowTempPage.aspx that is empty with code behind Page Load event:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(Session["TempPage"]);
}

Something like that could work..

Update: To open the temp page in a new window create a link like this on the main page:

<a href="http://localhost/ShowTempPage.aspx" target="_blank">View temp Page</a>

Upvotes: 2

Kees C. Bakker
Kees C. Bakker

Reputation: 33381

I would use an iframe and a piece of javascript. Use javascript and document.write to fill the iframe window.

Upvotes: 1

Richard
Richard

Reputation: 109015

Create an HTTP Handler, and in the implementation of IHttpHandler serve the HTML content from memory. Use the correct target attribute on the link to force new tab/window on the client.

(That's based on the asp.net tag on the question: you're writing a web site. If this is a local application (WinForms or WPF) then you can embed a browser control and set its NavigateToString method to the HTML text.)

Upvotes: 1

Related Questions