vba 查看本地文件时,在 MS Access WebBrowser Control 中禁用 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13338424/
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
Javascript disabled in MS Access WebBrowser Control when viewing local file
提问by Mike Lahey
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.
我正在尝试使用 WebBrowser 控件来显示使用 Google Maps JavaScript API 的本地保存的 HTML 文件,但 JavaScript 仍然顽固地禁用。
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
为了测试这个问题,我使用基于https://developers.google.com/maps/documentation/javascript/examples/map-simple 的谷歌地图制作了一个简单的页面。我还添加了一个按钮来直接测试 JavaScript 是否正常运行
c:\map-test.html works perfectly in both Firefox and IE.
c:\map-test.html 在 Firefox 和 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.
然后我创建了一个带有名为 cWebBrowser 的 WebBrowser 控件的 MS Access 表单,并添加了一个方法来加载 c:\map-test.html。谷歌地图界面无法加载,测试按钮已死。
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
接下来,我尝试了map-simple的在线版本。JavaScript 按预期工作并加载了 Google Map 界面
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
在这篇文章http://support.microsoft.com/?kbid=315933 之后,我在 IE 安全设置中启用了我的电脑区域
In all zones I enabled the following:
在所有区域中,我启用了以下功能:
- Allow scripting of Microsoft web browser control
- Active Scripting
- 允许编写 Microsoft Web 浏览器控件的脚本
- 活动脚本
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
- 允许 CD 中的活动内容在我的电脑上运行
- 允许活动内容在我的电脑上的文件中运行
Someone suggested adding Mark of the Web to my pages, but that didn't help either.
有人建议在我的页面中添加 Web 标记,但这也无济于事。
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?
map-test.html 在 IE 和 WebBrowser Control 中下载时都可以工作,但只能在本地使用 IE 工作。除了安全设置之外,还有什么可能导致问题?是否有我遗漏的安全设置?是否还有其他测试可以诊断问题?
I'm at my wits end.
我在我的智慧尽头。
Environment:
Windows 7 64bit
Access 2010
IE 9
环境:
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.
今天,我尝试创建一个非常简单的 JavaScript 页面,以确保它不是导致问题的 Google 地图代码。
<!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).
起初它奏效了,我很高兴。接下来,我尝试了谷歌地图测试页面,它也奏效了!所以,我尝试了我的全功能地图(在 IE 和 Firefox 中工作),它从我的数据库应用程序加载 JSON 输出,但它全部着火了,谷歌地图代码在内部导致了许多错误(不幸的是,我没有记录这些错误) .
Now, I'm back at square one; none of the pages allow scripting, including the 5 liner above ?!?!
现在,我回到了第一广场;所有页面都不允许编写脚本,包括上面的 5 行?!?!
采纳答案by Renaud Bompuis
First, have a look at Enhanced Protected Mode and Local Filesand 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 itsOnClick
event works:Private Sub Command1_Click() WebBrowser0.ControlSource = "=""C:\Users\Renaud\Desktop\map-simple.htm""" End Sub
创建一个新表单,向其中添加一个 webbrowser 控件,然后:
将其设置
ControlSource
为 URL 作品:下载 HTML 并保存到我的桌面,然后在
ControlSource
作品中引用本地文件:添加了一个按钮并
ControlSource
在其OnClick
事件工作中设置 Webbrowser :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 toC:\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 Webto 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
如果文件保存在用户的临时文件夹中,它将正确加载。
试试看,输入%TEMP%
资源管理器,它应该带你去C:\Users\username\AppData\Local\Temp
或其他类似的东西。
将 html 文件保存在那里并尝试从 Access 加载它,例如使用上面表单上的按钮:Private Sub Command1_Click() WebBrowser0.ControlSource = "=""C:\Users\Renaud\AppData\Local\Temp\map-simple.htm""" End Sub
或者,为了让 IE 允许您打开文件,您需要降低其完整性级别,以便在 IE 打开时强制在 Internet 区域中运行(从而使其正常工作)。您可以通过打开命令提示符(以管理员身份)检查文件的当前设置:
现在,我们可以设置文件完整性并再次检查设置:
现在您应该能够从 Access 和 IE 打开文件。
请注意,当您将 ACL 从 NTFS 系统移动到另一个 NTFS 系统时,ACL 会随文件一起传输,但是如果您将它们复制到 USB 闪存驱动器或其他格式化为 FAT 的系统,例如,它们可能会丢失,因此它们可能需要重新- 如果它们丢失,则应用于目标机器。
正如 Remou 所提到的,我尝试将Web 标记添加到文件本身,但它对我不起作用,文件无法加载。
这是我尝试过的:<!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"> ....
另一件应该起作用的事情是尝试从本地 http 服务器(例如Mongoose)提供文件
回答by Fionnuala
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"
.
使用 Windows 7 和 Access 2010 以及工具栏中的 Webbrowser 控件,而不是其他 ActiveX 控件;使用 OP 中的代码(如下)并添加Web 标记(MOTW);使用保存到桌面的 *.html 文档;将 webbrowser 控件源设置为="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>
回答by Tom Robinson
I've used the WebBrowser Control many times on Access forms, with locally-generated content.
我已经在 Access 表单上多次使用 WebBrowser Control 和本地生成的内容。
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.)
我的解决方案是使用 VBA 而不是 Javascript,对于本地运行的任何内容。您甚至可以将基于 VBA 的事件处理程序附加到文档元素。(使用对象浏览器探索 MSHTML 库。)
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.
这包括使用 file:// 加载的文件和通过导航到 about:blank 然后使用 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.
对于 Google 地图,您应该能够构建一个本地文档来托管基于 Internet 的 iframe。安全方面,Javascript 不能在本地文档中工作,但它应该在 iframe 中工作。
...Tom Robinson
...汤姆罗宾逊