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...
Related
The variable fieldName for testGetFieldName function is located at Code.gs
I want to insert it into the input value of the index.html file.
But if I open Sidebar using testSetValue, the value comes out as undefiend.
How can I get the variable 'Account' to come out with Value?
The code I wrote is as follows.
//code.gs
function testGetFieldName(){
var fieldName = 'Account';
return fieldName;
}
function testSetValue(){
var html = HtmlService.createTemplateFromFile('index');
var output = html.evaluate()
.setTitle('MySidebar')
.setWidth(400)
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showSidebar(output);
}
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input type="text" id ='textValue' name='textValue' value=''/>
<script>
document.getElementById('textValue').setAttribute('value',google.script.run.testGetFieldName());
</script>
</body>
</html>
In your script, how about the following modification?
Google Apps Script side:
From:
var html = HtmlService.createTemplateFromFile('index');
var output = html.evaluate()
To:
var html = HtmlService.createTemplateFromFile('index');
html.value = testGetFieldName();
var output = html.evaluate()
HTML side:
From:
document.getElementById('textValue').setAttribute('value',google.script.run.testGetFieldName());
To:
document.getElementById('textValue').setAttribute('value', '<?= value ?>');
Note:
If you want to use google.script.run, how about the following modification?
From
document.getElementById('textValue').setAttribute('value',google.script.run.testGetFieldName());
To
google.script.run.withSuccessHandler(e => {
document.getElementById('textValue').setAttribute('value', e);
}).testGetFieldName();
Reference:
HTML Service: Templated HTML
In general, it's a best practice to separate your HTML,CSS,JavaScript files.
References
HtmlService Best Practices
On my site mysite.com I load scripts from anothersite.com. Is there a way for a script running on mysite.com to know that it was downloaded from anothersite.com?
I found this to the the solution:
function serverName() {
var server = "";
//IE and EDGE can't use the case-insensitive 'i' in this selector
var path = $('script[src*="loader.js"]').attr('src'); //Look for the script tag of this script and get the URL
var regex = RegExp('.*\/\/.*?\/'); // gets this: http://mysite/
var m = regex.exec(path);
if (m && m.length) {
server = m[0].replace(/\/$/, "");//trim trailing slash
}
return server;
}
<!doctype HTML>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js" id="know"></script>
</head>
<body>
Hello
<script>
const gebi=id=>document.getElementById(id)
console.log(gebi('know').src)
</script>
</body>
</html>
This would work
const domain = location.hostname;
See more about location.hostname
The hostname property sets or returns the hostname of a URL.
I want to getElementsByTagName in xml file.
It is my code(.html).
<html>
<header>
<title>Read XML</title>
</header>
<body>
<h1>Hello My Application</h1>
<script type="text/javascript">
function readXML()
{
var xml= new XMLHttpRequest();
xml.open('GET', 'C:\Users\xxx\Testxml.xml');
//xml.send();
var xmlData = xml.responseText;
if(!xmlData)
{
xmlData = (new DOMParser()).parseFromString(xml.responseText, 'text/xml');
var emp = xmlData.getElementsByTagName("employee");
var name= emp[0].getElementsByTagName("name")[0].firstChild.data;
document.write("Name = " + name);
}
}
</script>
<button onclick="readXML()">Read XML File</button>
</body>
</html>
I run filename.html but there is error on line var name= emp[0].getElementsByTagName("name")[0].firstChild.data;
It is my xml file.
<company>
<employee>
<name>Chrish</name>
<age>40</age>
<salary>100</salary>
</employee>
</company>
Could you help me please?
Have you tried logging your xmlData variable to see if you are actually able to read the xml file? Because, as far as I know, reading local xml files in javascript is not allowed directly. You can try reading it through the File API. Read more about it here.
And secondly, your if condition seems incorrect. You are checking for (!xmlData) which means it will run when xmlData is empty while it should be running when you are actually able to get data in xmlData variable.
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||\"", "");
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>');