Reputation: 31
solution:
Javascript is not executed inside a webbrowser-control when designmode is set to "on", in my case it was:
document = webbrowser.Document as HTMLDocument;
document.designMode = "On";
the designmode property stopped the webbrowser-control executing javascript, so i changed it to the following:
document.body.setAttribute("contentEditable", "true");
now the javascripts are executed and editing the document ist still possible, so this simple change solved the problem for me. thanks for all replies
original question:
i am trying to implement a C# GUI with a WebBrowser-control. Within the Webbrowser i am calling local stored HTML-Files, the files are always stored on C: or D:, they are not on shared folders or something like that. Everything works fine so far.
Now i tried to add some JavaScript to the HTML-Files with some basic functions to edit html-tables (Add Row, Add Column etc). But the Javascript is not executed inside the webbrowser-control. I tried to open the files with IE and i get prompted for ActiveX contents, i tell IE "Allow blocked content" and everything works fine. (I dont get the prompt inside the webbrowser-control.) Now the Question: Is it possible to "always enable" any kind of scripting in a webbrowser-control or to prompt in the same way like IE does ?
Of course i searched the Web and Stackoverflow and i found several Solutions:
IE only prompts for local files. Javascript from any website (e.g. http://javatester.org/javascript.html) is executed. Adding the following Code to the local HTML-File should fix this problem (as mentioned here: https://msdn.microsoft.com/en-us/library/ms537628(v=vs.85).aspx). I tried it with the very basic example from the msdn site and it worked, but after adding some more Code it didnt work anymore. IE prompted me again, so this solution seemed not be reliable to me.
<!-- saved from url=(0014)about:internet -->
or
<!-- saved from url=(0023)http://www.contoso.com/ -->
Another solution was to change the security settings of IE, this should also affect the webbrowser-control. So i tried set all zones to the lowest security level (Internet options -> security), after that i clicked "custom level" to enable even more. But it still didnt work, i still got prompted. There was only one thing i did not understand: Open HTML-Files in the normal-mode prompted me for executing script, but the inprivate-mode immediatelly executed any script. Is this just a bug or normal behaviour? But finally changing security settings again brought no solution
The Invokescript Method inside my C# Programm could not run any scripts again.
Edit Tables directly (with execCommand("enableInlineTableEditing", true);
) doesnt work since this command is not supported by the IE.
So if you read until this point, i have to say thank you. And if you got any idea, how to reliable activate JavaScripts inside the webbrowser-control or just how to edit html-tables inside a webbrowser-control, i would be really happy :)
Btw: I am using .NET 4.5 and i tested all the solutions and two different PCs with different users. Finally i am sorry for my english, hope everyone could understand.
edit: Basic example, which works fine in IE (not even prompting) but its not executing any kind of JS in webbrowser-control:
<!doctype html>
<!-- saved from url=(0023)http://www.contoso.com/ -->
<html>
<head>
<title>A Mark of the Web Example test</title>
</head>
<body>
<p>Hello, World</p>
<script language="JavaScript">
document.write ("Hello Javascript");
</script>
<p>
alert test
</p>
<script language="JavaScript">
alert("test");
</script>
</body>
</html>
Upvotes: 1
Views: 1370
Reputation: 31
Javascript is not executed inside a webbrowser-control when designmode is set to "on", in my case it was:
document = webbrowser.Document as HTMLDocument;
document.designMode = "On";
the designmode property stopped the webbrowser-control executing javascript, so i changed it to the following:
document.body.setAttribute("contentEditable", "true");
now the javascripts are executed and editing the document ist still possible, so this simple change solved the problem for me. thanks for all replies
Upvotes: 1