I have a C#-WPF application which I placed a web browser in it. in the solution I have also index.html file and also a js file. the HTML has some calls to the javascript file.
and has the following tag:
<script type="text/javascript charset="utf-8" src="myfile.js"></script>
in my xaml.cs I have the following code:
webBrowser.Source = new Uri("hard-coded path to the HTML file");
when I run this code I get an error "An error has occured in the script on this page, object doesn't support property or method 'addEventListener'"
but when I clicked only on the same HTML file and run it in a broweser, there is no error.
what can cause this behavior? what is wrong in my C# code? Any idea??
thanks
I get an error "An error has occured in the script on this page,
object doesn't support property or method 'addEventListener'"
This error happens because you embded webbrowser could not find address of myfile.js.so for this problem you can put java scrip file in source
so do this:
<script type="text/javascript charset="utf-8" src="AddressLocation\myfile.js"></script>
Change with:
<script type="text/javascript>
//add content of your java file here
</script>
Related
I'm using HttpClient to get html string and use WebView's navigateToString method to show this page. I know I can use WebView load this page directly, but I would need to do some processing on that page before it's shown in WebView.
So, I faced a question. The web page quotes some css/js files in header, but I saw that 'href' value is relative path. Then the page will not show correctly in WebView.
[Updated]
For example, I'm using HttpClient to request a URI (http://example.com), then I will get the whole html page string. I will do some operations on this html string. After that, I will use WebView.NavigateToString(htmlpage) method to show this page. But if you check its head tag, there will be some <link> tag, its href value is relative path(/style-a/1.css), not absolute path. Then you will find that the html page doesn't show correctly in WebView.
Could someone give me a solution/code sample?
#Pedro Lamas, rene, Barett, moi_meme, Shachaf.Gortler Please do not put my question on hold. I didn't break any SO rules. My question was very clear. I think you do this, it's because you do not know how to answer my question. That's ok. If you don't know, you could choose not to answer it, but please do not put it on hold.
You can set address of your Link or Scripts tag with ms-appx-web:// and put address after that with additional [ / ] .
for example in this case you can use :
<script src="ms-appx-web:///Assets/FolderName/test.js" type="text/javascript"></script>
and also for link can use :
<link rel="stylesheet" type="text/css" href="ms-appx-web:///style-a/1.css">
I have an aspx page that needs to be an embedded resource for a WCF project. An interfacing service serves the aspx/html/js files to the web browser via a Stream object. The problem I am having is getting the aspx page to display properly. I have tried setting the content type to "text/html", but when I do this, I get the following at the top of the page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Services.Interface.Views.WebForm" %>
Which is the first line of Index.aspx.
Since it's being served as an html file, the aspx "stuff" is ignored, I guess.
I've also tried setting the content type to "application/xhtml+xm" as some have suggested, but I get an error that says:
This page contains the following errors:
error on line 1 at column 2: StartTag: invalid element name
Below is a rendering of the page up to the first error.
I have tried taking out the ASP stuff from Index.aspx and changing it to an HTML file, and it works, but I need the ASP capabilities for accessing some settings in Web.config.
Is there a proper content type for aspx pages that I'm not aware of, or is something else going wrong? Thanks in advance
I'm trying to port THIS pasge and its HTML5 animation to a winform.
What I did is copied and added the index.html and all the css and js file to my project. This is how my project directory looks like
And then I'm trying to load the html file into my webbrowser object using this code snippet
private void Form1_Load(object sender, EventArgs e)
{
string curDir = Directory.GetCurrentDirectory();
this.webBrowser2.Url = new Uri(String.Format("file:///{0}/index.html", curDir));
}
And I have set properties of all the file to Copy Always
Now when I run the project I get a script error message and upon pressing Yes
it loads the page but only background is displayed and the animation is lost!
please help!
PS: I've changed
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script src="js/ThreeWebGL.js"></script>
<script src="js/ThreeExtras.js"></script>
To
<link href="main.css" rel="stylesheet" type="text/css" />
<script src="ThreeWebGL.js"></script>
<script src="ThreeExtras.js"></script>
in index.html file
Screenshots of two script error that I'm getting--
It works in IE10
The *.js files need to be in a js folder. Either create the folder and move them or edit the html file.
EDIT1:
Does it work if you point it to "http://www.script-tutorials.com/demos/177/index.html"?
EDIT2:
Add:
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
in the html head section.
EDIT3:
I tried on my machine and it's the same behavior. But the page does run fine in Internet Explorer 11. Maybe the WinForms control uses IE engine in some compatibility mode.
I would use http://www.awesomium.com/
The webbrowser control is just an instance of IE - that page doesn't work for me in Internet Explorer so it's unlikely to work in the embedded browser. Search for webkit net - might give you better results. I'm tempted to ask why you want to do this but I'm sure you've got a good reason.
Got it working! Basically the set up works fine. Just had to open main.css file and comment out all the footer properties to get rid of the footer and found another script ref which was pointed to js/script.js had to change it to script.js
Now how can i increase the framerate so that it doesnt stutter? Theres a lag at the moment
I have small question that I can't quite figure out solution myself or find any answer (or clue) on web. Let me explain my problem.
I uploaded files to my SQL server using ASP.NET application.
The thing is, I'd like to view content of those files on aspx page.
Files are only code files (.cs, .aspx etc), basicaly txt in different technologies. I'd also like to use highlight.js to view those files as code.
Try something like this:
UI:
Import required hightlight required js/css files in .aspx page.
<link rel="stylesheet" href="/path/to/styles/default.css">
<script src="/path/to/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
Have one label/textarea to show code of .aspx/.aspx.cs file under
<pre><code class="cs"><asp:Label id="lblCode" runat="server"/></code><pre>
Server side:
In Page_Load() make db call to get content of .aspx/.aspx.cs files from database and set to label.
protected void Page_Load()
{
this.lblCode=DataAccessLayer.GetCode();
}
Check below links for more details about highlight.js
https://highlightjs.org/usage/
http://highlightjs.readthedocs.org/en/latest/css-classes-reference.html
Intro:
I'm working on a C# WinForms + WebBrowser project.
I'd like to do some processing upon a click on a hyperlink.
Here's how I hook a click event to every hyperlink in an HTML document:
void webBrowser_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e )
{
HtmlElementCollection links = webBrowser.Document.Links;
foreach ( HtmlElement link in links )
{
link.Click += delegate { MessageBox.Show( "C# Click!" ); };
}
}
The Problem:
The above code works well, but the problem is that I get side effects from javascript code which also performs some clicking action.
I learned the hard way that this problem comes in many shapes and forms..
Here's a not so obvious example of a jQuery action that makes life harder:
$(document).ready(function() {
$("a").click(function() {
alert("jQuery Click!");
});
});
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>>
</head>
<body>
Click Me!
</body>
</html>
<html>
<body>
What happens here is that when clicking a hyperlink in the WebBrowser control - the "jQuery Click!" alert will pop up first, and only after dismissing it will the "C# Click!" message be shown.
The Question:
I've searched far and wide, but didn't yet come up with an answer.
How could I C#-programmatically prevent jQuery or any other form of javascript code from being executed in the WebBrowser?
Clarification:
I'd like to say I already tried to alter the document in many ways, e.g. stripping <script> tags from webBrowser.DocumentText, and from the inner html node, and by working with HtmlElement and the underlying IHtmlElement objects...
But no luck..
Changing the webBrowser.DocumentText causes the document to get reloaded from a string, and thus I lose any credentials (e.g. cookie) I had to the original site. And changing IHtmlElement objects doesn't really make a difference 'cause the jQuery script was already loaded so, again, I am unable to prevent the event from occurring.
To my knowledge there is no way to disable JavaScript execution in WebBrowser component.
The only you can do is to suppress the error messages with ScriptErrorsSupressed property set to false.
An idea would be to pre-filter your HTML and eliminate all your "onclick" events from HTML using regular expressions before you render your HTML in the WebBrowser component.