Javascript disabled in MS Access WebBrowser Control when viewing local file

I have seen similar posts before, but none of the solutions I have seen online seem to work for me.

I am trying to use a WebBrowser Control to display a locally saved HTML file that uses the Google Maps JavaScript API, but JavaScript remains stubbornly disabled.

To test the problem, I made a simple page using Google Maps based on https://developers.google.com/maps/documentation/javascript/examples/map-simple . I also added a button to directly test if JavaScript was functioning

c:\map-test.html works perfectly in both Firefox and IE.

<!DOCTYPE html>
<html>
<head>
    <title>Google Maps JavaScript API v3 Example: Map Simple</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
        html, body, #map_canvas {
           margin: 0;
           padding: 0;
           height: 100%;
        }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
      var map;
      function initialize() {
        var mapOptions = {
          zoom: 8,
          center: new google.maps.LatLng(-34.397, 150.644),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
        mapOptions);
      }
      google.maps.event.addDomListener(window, 'load', initialize);
   </script>
</head> 
<body>
    <input type="button" onclick="alert('JavaScript Functioning');" value="Test JavaScript">
<div id="map_canvas"></div>
</body>
</html>

I then created an MS Access form with a WebBrowser Control named cWebBrowser and added a method to load c:\map-test.html. The Google Maps interface doesn't load and the test button is dead.

Private Sub Form_Current()
     Me.cWebBrowser.ControlSource = "=(""file:///C:/map-test.html"")"  
End Sub

Next, I tried the online version of map-simple. The JavaScript worked as expected and the Google Map interface loaded

Private Sub Form_Current()
     Me.cWebBrowser.ControlSource = "=(""https://google-developers.appspot.com/maps/documentation/javascript/examples/map-simple"")"
End Sub

I have been trying to solve this problem for quite a while without success.

Following this article, http://support.microsoft.com/?kbid=315933 , I enabled the My Computer Zone in IE security settings

In all zones I enabled the following:

  • Allow scripting of Microsoft web browser control
  • Active Scripting

Under the Advanced Tab I enabled the following:

  • Allow active content from CDs to run on My Computer
  • Allow active content to run in files on My Computer

Someone suggested adding Mark of the Web to my pages, but that didn't help either.

I have tried clearing the cache.

I have tried changing the filename, in case the cache remained after clearing.

map-test.html works when downloaded in both IE and the WebBrowser Control but only works locally using IE. What else could be causing the problem other than a security setting? Is there a security setting that I am missing? Is there any other test that I could do to diagnose the problem?

I'm at my wits end.

Environment: Windows 7 64bit Access 2010 IE 9

P.S.

The problem continues to get weirder

Today, I tried creating an extremely simple JavaScript page to ensure it wasn't the Google Maps code causing the problem.

<!DOCTYPE html>
<html>
    <body>
        <input type="button" onclick="alert('JavaScript Functioning');" value="Test JavaScript">
    </body>
</html>

It worked at first and I was elated. Next, I tried the Google Maps test page and that worked too! So, I tried my full-featured map (working in IE and Firefox) that loads JSON output from my DB application and it all went down in flames, Google Maps code internally caused numerous errors (unfortunately, I didn't document the errors).

Now, I'm back at square one; none of the pages allow scripting, including the 5 liner above ?!?!

Answers


First, have a look at Enhanced Protected Mode and Local Files and Understanding Local Machine Zone Lockdown.

What works on my machine

I've tested different configurations, using your exact same environment, and they all work (see caveats below).

  • Created a new form, add a webbrowser control to it then:

  • Set its ControlSource to the URL works:

  • Download the HTML and save it to my desktop, then reference the local file in the ControlSource works:

  • Added a button and set the Webbrowser's ControlSource within its OnClick event works:

    Private Sub Command1_Click()
        WebBrowser0.ControlSource = "=""C:\Users\Renaud\Desktop\map-simple.htm"""
    End Sub
    

Solutions

From the links to the articles about local webpage security I mentioned above, I tried a few things:

  • If the file in saved in the user's temp folder, it will load properly. Try it, type %TEMP% in Explorer and it should take you to C:\Users\username\AppData\Local\Temp or something equivalent. Save the html file there and try to load it from Access, for instance using the button on the form above:

    Private Sub Command1_Click()
       WebBrowser0.ControlSource = "=""C:\Users\Renaud\AppData\Local\Temp\map-simple.htm"""
    End Sub
    
  • Alternatively, for IE to allow you to open the file, you would need to lower its integrity level so it is forced to run in the Internet zone when IE opens it (and thus make it work properly). You can check the current settings for the file by opening the command prompt (as Administrator):

    Now, we can set the file Integrity and check the settings again:

    Now you should be able to open the file from Access and IE.

    Note though that the ACLs travel with the file when you move them from NTFS system to another NTFS system, but they could be lost if you copy them to a USB flashdrive or another system formatted as FAT for instance, so they may need to be re-applied on the target machine if they are lost.

  • I tried to add a Mark Of The Web to the file itself, as Remou mentioned, but it doesn't work for me, the file won't load. Here is what I tried:

    <!doctype html> 
    <!-- saved from url=(0014)about:internet -->
    <html>
      <head>
        <title>Google Maps JavaScript API v3 Example: Map Simple</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        ....    
    
  • One other thing that should work is to try to serve the files from a local http server, like Mongoose


What works on my machine

Purely for the sake of future readers.

Using Windows 7 and Access 2010 and the Webbrowser Control from the toolbar, not from the additional ActiveX controls; using the code (below) from the OP and adding Mark of the Web (MOTW); using a *.html document saved to desktop; setting webbrowser control source to ="C:\Users\<user>\Desktop\java.htm".

<!DOCTYPE html>
<!-- saved from url=(0023)http://www.contoso.com/ -->
<html>
<head>
    <title>Google Maps JavaScript API v3 Example: Map Simple</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
        html, body, #map_canvas {
           margin: 0;
           padding: 0;
           height: 100%;
        }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
      var map;
      function initialize() {
        var mapOptions = {
          zoom: 8,
          center: new google.maps.LatLng(-34.397, 150.644),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
        mapOptions);
      }
      google.maps.event.addDomListener(window, 'load', initialize);
   </script>
</head> 
<body>
    <input type="button" onclick="alert('JavaScript Functioning');" value="Test JavaScript">
<div id="map_canvas"></div>
</body>
</html>

I've used the WebBrowser Control many times on Access forms, with locally-generated content.

For portability, I don't want to mess with the security settings on each computer.

My solution is to use VBA instead of Javascript, for anything run locally. You can even attach VBA-based event handlers to document elements. (Use Object Browser to explore the MSHTML library.)

This includes both files loaded with file:// and on pages built by navigating to about:blank and then writing content with WebBrowser.Document.Open / .Write.

For Google Maps, you should be able to build a local document that hosts an Internet-based iframe. Security-wise, Javascript won't work in the local document but it should work in the iframe.

...Tom Robinson


Need Your Help

WSO2 ESB4.0.3 can not use XQUery

wso2 xquery wso2esb

In ESB4.0.0,I can use XQuery like this:

Android studio / Gradle strange behaviour

android-studio gradle android-gradle-plugin

I am experiencing a strange bug with Android Studio with new projects since yesterday (haven't even updated anything):