Controller
public ActionResult GetCategories()
{
var htmlText = new StringBuilder();
var scriptText = new StringBuilder();
htmlText.Append("Hello world");
scriptText.AppendFormat("document.write({0});", htmlText.ToString());
var content = new ContentResult();
content.Content = scriptText.ToString();
return content;
}
View
<script src="/Home/GetCategories" type="text/javascript" language="javascript"/>
It runs well on FF, but not in IE.
A script tag needs a closing tag to be compliant. IE actually obeys the standard in this respect while FF is more forgiving. Change your view to:
<script src="/Home/GetCategories" type="text/javascript" language="javascript">
</script>
Related
I have an HTML with a Javascript code that tries to open a named window if not exists, creates a form, and than posts to that existing window, using its target attribute.
I have a C# Form, with 2 webbrowsers controls in it, side by side, and than I open this HTML in browser1, sets the browser2.Document.Window.Name = "janelaoperacao", the target of the HTML form, and try to post the HTML form, and it opens a new IE window, instead of showing the result in browser2 window.
When I set browser1.Document.Window.Name="janelaoperacao", the posted HTML form shows up normaly on the browser1 window;
So, my problem is browser1.Document is not viewing the browser2.Document named window, and is trying to open another IE window instead. Am I missing some directives, so they can expose to each other inside the same form?
HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Teste Java Script Window.Open identificada</title>
<script type="text/javascript">
var PostObj = (function () {
var hiddenForm;
var i;
var createHiddenForm = function () {
hiddenForm = document.createElement("form");
hiddenForm["style"].visibility = "hidden";
hiddenForm["method"] = "post";
hiddenForm["target"] = "janelaoperacao";
hiddenForm["action"] = sucesso.html;
hiddenForm["name"] = "hiddenForm";
hiddenForm["id"] = "hiddenForm";
document.body.appendChild(hiddenForm);
};
this.submit = function (urlDestino) {
createHiddenForm();
document.getElementById("hiddenForm").submit();
};
return this;
})();
function abreJanelaIdentificada(){
var f = window.open("", "janelaoperacao", "location=no");
}
</script>
</head>
<body>
<div id="idPaginaRealizaCopia">
<script type="text/javascript">
//<![CDATA[
function fazTudo(){
abreJanelaIdentificada();
PostObj.submit("file:///D:/temp/sucesso.html?teste=2");
}
//]]>
</script>
<form>
<input type="button" name="botao" value="Abrir" onclick="fazTudo();" />
</form>
</div>
</body>
And the WebBrowser code that does not work (it opens a new IE window):
//after creating the form and stuff -> no errors before navigate, nor NullExceptions
browser1.Navigate("file:///D:/temp/teste.html");
browser2.Document.Window.Name = "janelaoperacao";
But, if I replace browser2 for browser1, it show the success message in the browser1 window (browser1.Document.Window.Name = "janelaoperacao")
I am using Google Prettify to generate a JavaScript document preview.
The plugin works in Chrome (if I refresh the page 2 to 3 times), but not in IE9 and IE11.
Due to dynamic rendering, I can't set onload="prettyPrint();" on my body tag, so I am using
<script>window.onload = prettyPrint </script>
I also tried this, with the same result
<script>
$(document).ready(function () {
prettyPrint()
});
</script>
I tried to debug run_prettify.js but it says undefined
var prettyPrint;
Here is the documentation
calling js file in end of body.
<script src="${url.context}/res/components/preview/code-prettify-master/prettify.js?autoload=true&lang=css'" />
<script src="${url.context}/res/components/preview/code-prettify-master/run_prettify.js" />
<script src="${url.context}/res/components/preview/code-prettify-master/lang-css.js" />
this is the code, where i am generating <pre> tags
var resultDiv = YAHOO.util.Dom.getElementsByClassName("previewer CodePrettifyMaster")[0];
var preDiv = document.createElement('pre');
preDiv.id = this.wp.id + '_prettify';
preDiv.className = "prettyprint lang-scm linenums";
var ganttContentDiv = document.createElement('div');
ganttContentDiv.id = 'contentContainer';
ganttContentDiv.appendChild(preDiv);
resultDiv.appendChild(ganttContentDiv);
I have a HTML page in my .Net project in that I have one script section
<script language="javascript" type="text/javascript">
s.property1="|*|property1|*|"
s.property2="|*|property2|*|"
<script>
In the c# code I'm reading the html file and replacing the "| * |property1| *|" values like below
StringBuilder siteCode = new StringBuilder();
//code to append html file to StringBuilder(siteCode)
if(xyz!=null)
{
siteCode.Replace("|*|property1|*|", xyz);
}
else
{
//remove s.property1="|*|property1|*|"
}
How can I remove s.property1="||property1||" from StringBuilder(siteCode)?
Just use the StringBuilder Replace method?
siteCode.Replace("s.property1=\"||property1||\"", "");
When I try to pass my Html into WebBrowser.DocumentText which include JS.
<script src="jquery\script.js"> </script>
It is same path with my executable. But when open in Winform, it is unable to find the 'script.js'..
But when you enter the full path of script, it is working.
StreamReader stringReader = new StreamReader(htmlFilename, Encoding.Default);
StringBuilder sb = new StringBuilder();
string temp;
while (!String.IsNullOrEmpty(temp = stringReader.ReadLine()))
{
sb.AppendLine(temp);
}
stringReader.Close();
return sb.ToString();
Eventually I read html into StringBuilder, and pass it to WebBrowser.DocumentText. Any other way to make it works without giving full path of the script?
You can give like this into your HTML document
<script type="text/javascript" src="../jquery\script.js"></script>
in HTML Page
<html>
<head>
<script type="text/javascript" src={Fullpath}></script>
</head>
<body>
</body>
</html>
in C#
string ScriptfullPath = Application.StartupPath.ToString()+"\\jquery\\script.js";;
string htmlContent;
using (StreamReader reader = new StreamReader(Application.StartupPath + \\JQuery\\sample.htm"))
{
htmlContent = reader.ReadToEnd();
}
htmlContent = htmlContent.Replace("{Fullpath}", ScriptfullPath);
Then your HTML document will use Script file with full path...
I have a HTML page in which I have a button; pressing that button a javascript function is called - here results a String which is the representation of an xml. I want to represent this xml on the same page with the button, similar with what is in the picture below:!
Here is the simplified code I've tried but did not worked (see under the code the result of it - nothing displayed):
<html>
<head>
<script type="text/javascript">
function xml_test()
{
var xmlString = "<note><name>Kundan Kumar Sinha</name><place>Bangalore</place><state>Karnataka</state></note>";
var my_div = document.getElementById("labelId");
alert(xmlString)
my_div.innerHTML += xmlString;
}
</script>
</head>
<body>
<input type="button" value="TEST" onclick="xml_test()"/>
<br><br>
<label id="labelId">XML: </label>
</body>
</html>
I've tried with an iframe also, but I do not have an file for the src attribute.
What I've tried is:
<html>
<head>
<script type="text/javascript">
function populateIframe() {
var xml = "<?xml version='1.0' encoding='UTF8' standalone='yes'?><note><name>Kundan Kumar Sinha</name><place>Bangalore</place><state>Karnataka</state></note>";
var iframe = document.getElementById('myIframe');
var idoc= iframe.contentDocument || iframe.contentWindow.document; // IE compat
idoc.open("text/xml"); // I know idoc.open(); exists but about idoc.open("text/xml"); I'm not sure if exists;
idoc.write('<textarea name="xml" rows="5" cols="60"></textarea>');
//idoc.write(xml); // doesn't work
idoc.getElementsByTagName('textarea')[0].value= xml;
idoc.close();
}
</script>
</head>
<body onload="populateIframe();">
<iframe id="myIframe" width="900" height="400"></iframe>
</body>
</html>
and the result is:
I've already looked over How to display XML in a HTML page as a collapsible and expandable tree using Javascript?
I took some ideas from here
Thank you for helping me!
Just Create am HttpHandler, and open it in a Iframe:
public class Handler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
XmlSerializer serializer = new XmlSerializer(typeof(Note));
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream, Encoding.Unicode);
serializer.Serialize(writer, new Note() { Name = "Kundan Sinha", Place = "Bangalore", State = "Karnataka" });
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
UnicodeEncoding utf = new UnicodeEncoding();
stream.Close();
writer.Close();
context.Response.ContentType = "text/xml;charset=utf-8";
context.Response.Write(utf.GetString(arr).Trim());
context.Response.Flush();
}
#endregion
}
public class Note
{
public string Name { get; set; }
public string Place { get; set; }
public string State { get; set; }
}
You can pass your received xml string to this where I am doing
context.Response.Write('Pass you XML data here');
You must use your favorite JavaScript library with a tree widget to display that XML in tree form.
Note that the "tree-like" view you see is actually IE's default view for XML files. Other browsers will have different views for XML files, and some do not even let you view XML files without a plug-in.
You should not depend on browser-specific functionality if viewing the XML in tree form is important to your page's functionality.
If you, however, just want to press a button and then the whole page gets turned into an XML, then by all means just redirect to that XML URI on button press. IE will show that XML file in tree form, while other browsers may either ask you to download the file, or display the XML file in whatever format that is determined by their plugin's.
I set the xml data in the src attribute:
iframeElement.setAttribute('src', 'data:text/xml,<test>data</test>');