开发者

Local load of HTML/Javascript file in C# WebBrowser object not loading correctly?

I'm using .NET Framework 4.0 (with WPF) trying to load a local HTML file within a WebBrowser object (System.Windows.Controls.WebBrowser) with both locally embedded javascript and loaded from a remote server. The problem is, the javascript (ajax with dojo) isn't executing inside the WebBrowser object when loaded:

webBrowser.NavigateToString(LoadStringFromFile("map.html"));

However, when loaded remotely it runs just fine as follows:

webBrowser.Navigate("http://www.example.com/map.html");

Sample excerpt code of Javascript code in html file:

<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>
<script type="text/javascript">
dojo.require("esri.map");

va开发者_C百科r map;

var colorRGB = { "white": [255,255,255], "red": [255,0,0], "blue": [0,255,0] };

function init()
{
    var streetLayer = new esri.layers.ArcGISTiledMapServiceLayer(
        "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");

    var extent = new esri.geometry.Extent(-140.910, 11.267, -53.019, 64.002);
    map = new esri.Map("map", { extent:extent });

    map.addLayer(streetLayer);

    dojo.connect(map,"onLoad", processLocations);
}

Anyone have any idea what is wrong with this?

My research suggest it has to do with IE and internet zones, but have been unable to confirm it.


When i paste this into an html file and open on internet explorer, it display a message that it blocked activex/script content. Change Internet Options > Advanced > Security > Allow active content to run in files on My Computer. If the warning goes away in IE, then it should work in WebBrowser object


The WPF WebBrowser class has a static initializer that enables FEATURE_LOCALMACHINE_LOCKDOWN for the entire process. You can use CoInternetSetFeatureEnabled to turn FEATURE_LOCALMACHINE_LOCKDOWN back off on the WebBrowser Navigating event.

This solution does not require you to modify the HTML files being displayed, but does disable some security features that you probably don't want when displaying trusted local content. See http://technet.microsoft.com/en-us/library/cc782928(v=ws.10).aspx for more information about local machine lockdown.

See How to disable click sound in WebBrowser Control for PInvoke and https://github.com/TaoK/PoorMansTSqlFormatter/blob/d6b4f7bedc02ce1bf59acb16dd1f49609c216aa7/PoorMansTSqlFormatterDemo/FrameworkClassReplacements/CustomContentWebBrowser.cs for an example usage.


Here is my solution for preventing scriptwarnings:

WebBrowser.ScriptErrorsSuppressed = true;

Also I've put my website as a trusted website in my Internet Settings, I don't know if that helps but the scriptErrorsSuppressed property was enough for me.

Hope this helps.


For me, the problem was:

Steps to enable ActiveX controls in Internet Explorer:

1.Select Tools --> Internet Options menu from the Internet Explorer.

2.Select the Security tab from the Internet Options dialog.

3.Select the appropriate Web content zone and click Custom Level.

4.Make the following options available under ActiveX controls and plug-ins to either enableor Prompt:1.Download signed ActiveX controls

2.Run ActiveX controls and plug-ins

3.Script ActiveX controls marked safe for scripting

5.Click Ok to save the security settings.

6.Click OK to save and close the Internet Options dialog.

Then click don't show this message again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜