Reputation: 333
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:
Upvotes: 0
Views: 993
Reputation: 1281
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.
Upvotes: 1