问题描述
我需要在我的Electron应用程序中渲染 iframe
:
I need to render iframe
in my Electron application:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<iframe sandbox='allow-scripts' src='frm.html'></iframe>
</body>
</html>
其中 frm.html
链接本地文件脚本 foo.js
这是我的Electron应用程序的一部分
where the frm.html
links the local file script foo.js
which is part of my Electron application
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<script src="foo.js"></script>
</head>
<body>
<p>Inside iframe</p>
</body>
</html>
当我在Electron中运行应用程序时,我可以在devtools控制台中看到此错误
When I run the application in Electron I can see this error in devtools console
Not allowed to load local resource: file:///C:/electron/app1/foo.js
Electron会出现这种情况吗?
Is it possible such scenario in Electron?
推荐答案
这是iframe的安全功能。这是一个类似的问题,讨论如何将链接文件加载到iframe中:。
This is a security feature of the iframe. Here is a similar question that talks about loading linked files into an iframe: Displaying local htm file in iframe?.
话虽如此,您是否考虑过使用webview标签? 。 webview标记与iframe非常相似,但可以让您更有能力控制它周围的安全性。我测试将本地文件加载到webview中的方式与尝试将frm.html加载到iframe中的方式相同,并且它可以正常运行。
That being said, have you considered using the webview tag instead? http://electron.atom.io/docs/v0.30.0/api/web-view-tag/. The webview tag is very similar to an iframe, but gives you more ability to control the security around it. I tested loading a local file into a webview in the same way you attempt to load frm.html into the iframe and it works flawlessly.
这篇关于iframe与Electron中的本地资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!