如何在没有URL或文件的情况下将代码馈送到iframe中?

发布于 2025-02-13 00:46:04 字数 437 浏览 0 评论 0原文

我正在尝试创建一个视觉编辑器,其中用户创建3D场景,而我的应用程序为项目本身编写了代码。我想创建一个测试功能,其中正在运行生成的代码。我已经考虑使用iframes,但据我所知,您需要一个特定的url iframe

以下是我想到的一些想法/解决方案:

  • 是否有一种方法可以将代码手动馈送到iframe,而没有文件或URL?
  • 还有其他更轻松的方法可以完成此任务吗?
  • 可以在没有服务器端代码的情况下完成此任务?

让我知道您是否需要更多信息。谢谢!

I'm trying to create a visual editor where the user creates a 3D scene, and my application writes the code for the project itself. I want to create a testing feature where the code that was generated is run. I've looked into using iframes, but as far as I know, you need a specific URL for iframe.

Here are some ideas/solutions I've come up with:

  • Is there a way to manually feed code to an iframe, without a file or URL?
  • Is there any other easier way to accomplish this task?
  • Can this be done without server side code?

Let me know if you need any more information. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的新欢旧爱 2025-02-20 00:46:04

是的,有可能只需在服务器/主机上的某个地方创建另一个HTML文件,而只是禁止外部访问它,以便只能从iframe中访问。

您必须使用脚本将代码提供给iframe而无需这样的源文件:

<iframe id="my-iframe"></iframe>

<script>
document.getElementById("my-iframe").contentDocument.write("<h1>Hello World</h1>");
</script>

将javascript更改为iframe>:

var iframe = document.getElementById("my-iframe");
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

iframeDocument.body.innerHTML = "<h1>Goodbye World</h1>";

此覆盖:此覆盖iframe的所有内容。

这将起作用,但要从iframe中格式化HTML将是一个很大的痛苦。据我所知,这是适合您需求的最简单方法。

Yes it is possible however a much simpler way would be to just create another html file somewhere on your server/host and just disallow external access to it so it's only accessible from the iframe.

You have to use an script to give code to an iframe without a source file like this:

<iframe id="my-iframe"></iframe>

<script>
document.getElementById("my-iframe").contentDocument.write("<h1>Hello World</h1>");
</script>

Change the JavaScript to this to overwrite all of the content of the iframe:

var iframe = document.getElementById("my-iframe");
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

iframeDocument.body.innerHTML = "<h1>Goodbye World</h1>";

This overwrites all the content of the iframe with your own.

This will work but it will be a big pain to format the html from the iframe. Also as far as I know this is the simplest way that fits your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文