0

I have a html file contains html:

<html>
    <body>
        <div class="infoLabel">*Your order has been placed in production queue</div><br />
        <div class="infoLabel">*The order confirmation will be delivered to you shortly</div> 
    </body>
</html>

In .net C# code behind I write the following codes to convert the html to string and pass to Javascript:

string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), "Templates/SiteNotifications/SavedQuote.html");
string htmlBody = File.ReadAllText(path);
ScriptManager.RegisterStartupScript(Page, GetType(), "changePriceList" + Guid.NewGuid(),"displayViewDetails(" + htmlBody + ");", true);

I found the htmlBody contains the string with proper escape. The Script is:

function displayViewDetails(currentOrder) {
    var w = window.open('_blank', "NewWindow", "width=" + width + ", height=" + height);
    w.document.open();
    w.document.write(currentOrder);
    w.document.close();
}

However, when debugging I found there is error for the Javascript to parse the html. I don't how to correct pass the html for the Javascript to parse. I found the existing posts don't seem to solve my problem. Anyone can help?

The error snapshot when debugging with IE: enter image description here

2
  • What does the html string look like when its passed into displayViewDetails Commented Apr 27, 2017 at 5:07
  • The string is "\r\n<html>\r\n<body>\r\n <div class=\"infoLabel\">*Your order has been placed in production queue</div><br />\r\n <div class=\"infoLabel\">*The order confirmation will be delivered to you shortly</div>\r\n</body>\r\n</html>\r\n" Commented Apr 27, 2017 at 5:17

1 Answer 1

1

The issue occurs because when the file is parsed its adding the new line characters to the string :\r\n. Those are not valid html markups. The solution is to remove the new line characters before passing them to the javascript function. You can do something like this:

string htmlBody = File.ReadAllText(path).Replace(Environment.NewLine, " ");

Note Environment.NewLine is the new line character for a given platform, from .NET documentation:

A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.

Sign up to request clarification or add additional context in comments.

2 Comments

It works! Thanks Mate! By the way, do you know if "File.ReadAllText" require garbage collection manually? Because I didn't call any .close() method. I do read the msdn document that the file being read will be close.
No problem! Yes i believe it closes it after its done reading, you don't have to close it yourself. Also, if the answer solves the question asked, when you get the chance, please mark it as accepted. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.