javascript 如何将本地脚本文件添加到 WebBrowser 控件的 HTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4029602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add a local script file to the HTML of a WebBrowser control?
提问by IAmAN00B
This seems really dumb. I've tried a bunch of different ways and it's just not working. I have a WinForms app with a WebBrowser control. If I try with a raw html file on my desktop using the same src string, the src I put together works fine. But plugging the same stuff into the WebBrowser control won't work.
这看起来真的很愚蠢。我尝试了很多不同的方法,但都行不通。我有一个带有 WebBrowser 控件的 WinForms 应用程序。如果我尝试在桌面上使用相同的 src 字符串处理原始 html 文件,则我放在一起的 src 可以正常工作。但是将相同的内容插入 WebBrowser 控件将不起作用。
Here's my code:
这是我的代码:
HtmlElementCollection head = this.wbPreview.Document.GetElementsByTagName( "head" );
if (head != null)
{
HtmlElement elm = this.webBrowserControl.Document.CreateElement("script");
string mySource = Environment.CurrentDirectory + @"\MyScriptFile.js";
elm.SetAttribute("src", mySource);
elm.SetAttribute("type", "text/javascript");
((HtmlElement)head[0]).AppendChild(elm);
}
The WebBrowser doesn't get the script. However, if I change "mySource" to an external resource (via http://), it works fine!
WebBrowser 没有获取脚本。但是,如果我将“mySource”更改为外部资源(通过 http://),它工作正常!
Help!
帮助!
采纳答案by womd
i came up on your post, while playing around with things following worked for me:
我想到了你的帖子,同时玩弄以下对我有用的东西:
HtmlElementCollection head = webBrowser1.Document.GetElementsByTagName("head");
if (head != null)
{
HtmlElement elm = webBrowser1.Document.CreateElement("script");
elm.SetAttribute("type", "text/javascript");
elm.InnerText = System.IO.File.ReadAllText(Environment.CurrentDirectory + @"\helperscripts.js");
((HtmlElement)head[0]).AppendChild(elm);
}
, so all methods of helperscript.js can be invoked using
, 所以 helperscript.js 的所有方法都可以使用
webBrowser1.Document.InvokeScript("methodname");
, here as a reference for the script invoke: How to inject Javascript in WebBrowser control?
,这里作为脚本调用的参考:如何在WebBrowser控件中注入Javascript?
greetings
问候
回答by SLaks
Try adding file://to the URL.
尝试添加file://到 URL。
回答by ikutsin
There is a long storyabout workarounds of that "security fix" from MS. New behavior was implemented starting from IE7. Take a look into "base" tag and IE Feature controls.
关于 MS 的“安全修复”的解决方法有很长的故事。从 IE7 开始实施了新行为。查看“基本”标签和IE 功能控件。
I did the following:
我做了以下事情:
//TODO: if not mono
var executableFilename = Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location);
var keys = new[] { executableFilename, [vsname]+".vshost.exe" }; //check!
Action<string, object, string> SetRegistryKeyOrFail =
(key, val, regStr) =>
{
var reg =
Registry.CurrentUser.CreateSubKey(regStr);
if (reg == null) throw new Exception("Failed registry: " + regStr);
reg.SetValue(key, val);
};
foreach (var key in keys)
{
SetRegistryKeyOrFail(key, 1, @"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BLOCK_LMZ_IMG");
SetRegistryKeyOrFail(key, 0, @"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BLOCK_LMZ_SCRIPT");
}
回答by Johnydep
This is because of security reasons. You need a webserver to do that, else you can access any file on a system which would be a big security hole.
这是出于安全原因。你需要一个网络服务器来做到这一点,否则你可以访问系统上的任何文件,这将是一个很大的安全漏洞。
In developement mode, you can set e.g on chrome:
在开发模式下,您可以设置例如在 chrome 上:
chrome.exe --allow-file-access-from-files
And you will be able to run your code.
您将能够运行您的代码。

