Writing html in a string
Seems like you're using JSF. Try this:
<div id="display">
<h:outputText value="#{PortalView.fullLeadData}" escape="false"/>
</div>
How to render HTML in string with Javascript?
You can render HTML using document.write()
document.write('<html><body><h2>HTML</h2></body></html>');
But to append existing HTML string, you need to get the id of the node/tag under which you want to insert your HTML string.
There are two ways by which you can possibly achieve this:
- Using DOM -
var tag_id = document.getElementById('tagid');
var newNode = document.createElement('p');
newNode.appendChild(document.createTextNode('html string'));
- Using innerHTML -
var tag_id = document.getElementById('tagid');
tag_id.innerHTML = 'HTML string';
Alternative to HTML in String
You could use template engines. This is at the expense of elements in the page, but the code will look much cleaner and the template easier to understand as HTML. Put the template in a script tag with type
set to text/template
<script type="text/template" id="tmpl">
<div class="form-group">
<label for="page-title">Title</label>
<input type="text" class="form-control" id="page-title" value="{{pageTitle}}">
</div>
</script>
And modify your function as below. Remember to cache the template.
var template = document.getElementById('tmpl').innerHTML; // cache the HTML
function createPageSettingsPopup(page) {
// Find an replace the {{pageTitle}} with page.title, and then return the HTML string.
return template.replace(new RegExp('{{pageTitle}}', 'gi'), page.title)
}
var template = document.getElementById('tmpl').innerHTML; // cache the HTMLfunction createPageSettingsPopup(page) { // Find an replace the {{pageTitle}} with page.title, and then return the HTML string. return template.replace(new RegExp('{{pageTitle}}', 'gi'), page.title) }
console.log( createPageSettingsPopup({title:'Hello World'}));
<script type="text/template" id="tmpl"> <div class="form-group"> <label for="page-title">Title</label> <input type="text" class="form-control" id="page-title" value="{{pageTitle}}"> </div></script>
how can i output to screen html as string?
Don't use document.write()
; it's the 20th-century way to build the DOM dynamically. Instead, put the string in the textContent
of an element that you created in the initial HTML.
var string = "<p>hi</p>";document.getElementById("output").textContent = string;
<div id="output"></div>
How to render HTML string as real HTML?
Check if the text you're trying to append to the node is not escaped like this:
var prop = {
match: {
description: '<h1>Hi there!</h1>'
}
};
Instead of this:
var prop = {
match: {
description: '<h1>Hi there!</h1>'
}
};
if is escaped you should convert it from your server-side.
The node is text because is escaped
The node is a dom node because isn't escaped
Write HTML string in JSON
You should escape the characters like double quotes in the html string by adding "\"
eg: <h2 class=\"fg-white\">
How do you convert Html to plain text?
If you are talking about tag stripping, it is relatively straight forward if you don't have to worry about things like <script>
tags. If all you need to do is display the text without the tags you can accomplish that with a regular expression:
<[^>]*>
If you do have to worry about <script>
tags and the like then you'll need something a bit more powerful then regular expressions because you need to track state, omething more like a Context Free Grammar (CFG). Althought you might be able to accomplish it with 'Left To Right' or non-greedy matching.
If you can use regular expressions there are many web pages out there with good info:
- http://weblogs.asp.net/rosherove/archive/2003/05/13/6963.aspx
- http://www.google.com/search?hl=en&q=html+tag+stripping+&btnG=Search
If you need the more complex behaviour of a CFG I would suggest using a third party tool, unfortunately I don't know of a good one to recommend.
C# - object-oriented way of writing HTML as a string?
Something like this?
private string getHTMLString()
{
DirectoryInfo di = new DirectoryInfo("some directory");
FileInfo[] files = di.GetFiles("*.dll", SearchOption.AllDirectories);
StringBuilder sb = new StringBuilder();
sb.Append("<table>");
foreach (FileInfo file in files)
{
Assembly assembly = Assembly.LoadFile(file.FullName);
string version = assembly.GetName().Version.ToString();
sb.AppendFormat("<tr><td>{0}</td><td>{1}</td></tr>", file.FullName, version);
}
sb.Append("</table>");
return sb.ToString();
}
Not really "object oriented" but I would argue the most logical.
DISCLAIMER: Compiled by hand
How to convert a HTMLElement to a string
You can get the 'outer-html' by cloning the element,
adding it to an empty,'offstage' container,
and reading the container's innerHTML.
This example takes an optional second parameter.
Call document.getHTML(element, true) to include the element's descendents.
document.getHTML= function(who, deep){
if(!who || !who.tagName) return '';
var txt, ax, el= document.createElement("div");
el.appendChild(who.cloneNode(false));
txt= el.innerHTML;
if(deep){
ax= txt.indexOf('>')+1;
txt= txt.substring(0, ax)+who.innerHTML+ txt.substring(ax);
}
el= null;
return txt;
}
Related Topics
Retrieve an Object from Entityframework Without One Field
Sort Datagridview Columns in C#? (Windows Form)
Deploying ASP.NET Web Forms Project to Fedora 24
System.Data.Sqlclient.Sqlexception: Invalid Column Name 'Phone_Types_Phone_Type_Id'
C# Null Coalescing Operator Equivalent for C++
How to Access Resourcedictionary in Wpf from C# Code
Linq: How to Exclude Condition If Parameter Is Null
Is Idependencyresolver an Anti-Pattern
Entity Framework the Underlying Provider Failed on Open
Why Must C# Operator Overloads Be Static
How to Get the Value of a Session Variable Inside a Static Method
Download a File from Azure Devops Server Writes Wrong Data to The File
Decrypt String in C# That Was Encrypted with PHP Openssl_Encrypt