11

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.

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!

Help!

IAmAN00B
  • 1,913
  • 6
  • 27
  • 38

4 Answers4

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

webBrowser1.Document.InvokeScript("methodname");

, here as a reference for the script invoke: How to inject Javascript in WebBrowser control?

greetings

Community
  • 1
  • 1
womd
  • 3,077
  • 26
  • 20
  • 1
    getting this error : {"Property is not supported on this type of HtmlElement."} – Furkan Gözükara Jul 21 '13 at 12:45
  • It works, but when trying to set a big script to `elm.InnerText`, the process just stops responding for quite a while. – Gildor Jun 04 '15 at 20:49
  • Depending on your Windows Feature Emulation Version (older ie) `elm.InnerText` may fail. Instead we can use `elm.SetAttribute("TEXT", ... )` – clamchoda Jun 16 '20 at 22:15
4

Try adding file:// to the URL.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I actually did that and have attempted all other known nomenclatures I was able to find. I've used Absolutes, Relatives, using URI's instead, etc. No go. – IAmAN00B Oct 27 '10 at 13:17
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.

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");
                }
ikutsin
  • 1,118
  • 15
  • 22
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.

In developement mode, you can set e.g on chrome:

chrome.exe --allow-file-access-from-files  

And you will be able to run your code.

Johnydep
  • 6,027
  • 20
  • 57
  • 74