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!
4 个解决方案
#1
11
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");
,這里作為腳本調用的參考:如何在WebBrowser控件中注入Javascript?
greetings
#2
4
Try adding file:// to the URL.
嘗試將file://添加到URL。
#3
1
There is a long story about 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 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");
}
#4
0
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.
這是出於安全原因。您需要一個Web服務器來執行此操作,否則您可以訪問系統上的任何文件,這將是一個很大的安全漏洞。
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.
您將能夠運行您的代碼。