使用 javascript 嵌入动态大小的网页

发布于 2024-11-30 02:24:48 字数 819 浏览 0 评论 0原文

好的,首先是一些背景信息:我正在尝试将网页嵌入到另一个页面中。子页面基本上是一个用 javascript 和 html 编写的小型 Web 应用程序,它接受多个输入屏幕(单选按钮、文本框等),并在最后给出一个带有结果的屏幕。每个屏幕的尺寸都可以不同。

我尝试使用两种方法来进行嵌入:

1)将子页面中的所有 html 和 javascript 复制到主页面中,并将其粘贴在 div/table/whatever 中。

2) 将子页面保留在自己的文件中,并使用 embed/object/iframe 嵌入它。

使用第一种方法,页面将按其应有的方式运行;唯一真正的问题(除了有点混乱的解决方案之外)是我嵌入的子页面实际上是由外部应用程序生成的,并且页面经常被更新的版本替换。这或多或少排除了使用第一种方法作为长期解决方案的可能性。

现在第二种方法有其自身的问题。由于嵌入的 javascript 页面的高度发生变化,因此容纳该页面的框架的大小也需要随之变化。我可以使用此处提供的任何解决方案更改大小,然而,当用户在每个屏幕上前进时,这些不会更新框架的大小。

到目前为止,我能想到的最接近的解决方案是使用 document.onclick 处理程序来捕获可能导致子页面的下一个屏幕出现的任何单击。处理程序暂停很短的时间(以允许出现下一个屏幕),然后调用必要的调整大小函数。然而,这感觉像是一个非常hacky的解决方案,并且当滚动条尚未扩展以适应新内容时,滚动条显示在框架的一侧,并且滚动条出现了轻微明显的延迟。我想一定有更好的方法来做到这一点。

Okay, so first some background info: I am trying to embed a webpage within another page. The sub-page is basically a small web application written in javascript and html that takes in several screens of input (radio buttons, text boxes, etc.) and gives a screen with results at the end. Each of these screens can be a different size.

There are two methods I have tried using to do the embedding:

1) Copy all of the html and javascript from the sub-page into the main page and stick it in a div/table/whatever.

2) Keep the sub-page in its own file and embed it using embed/object/iframe.

Using the first method the page behaves as it should; the only real problem (aside from being kind of a messy solution) is that the sub-page I am embedding is actually generated by an external application, and every so often the page is replaced with a newer version. This more or less rules out using the first method as a long-term solution.

Now the second method has its own problems. Since the embedded javascript page changes in height, the frame that is holding it needs to vary in size with it. I'm able to change the size using any of the solutions given here, however these do not update the size of the frame as the user progresses through each screen.

The closest solution I've been able to come up with so far is using a document.onclick handler to catch any clicking which might cause the next screen of the sub-page to come along. The handler pauses for a very short time (to allow the next screen to come up) and then calls the necessary resizing function. However this feels like a very hacky solution, and there is also a slightly noticeable delay during with the scroll bar shows up on the side of the frame when it hasn't expanded yet to fit the new content. I'm thinking there must be a better way to do this.

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

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

发布评论

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

评论(1

稀香 2024-12-07 02:24:48

如果文件位于同一服务器/域上,您可以使用 jQuery 加载它。下面是一些 jQuery 代码:

<script type="text/javascript">
$(document).ready(function() {
$('#id-of-div').load('/path/to/page.html');
});
</script>

只需将 id-of-div 更改为您希望页面加载到的 div 的 id 并更改 /path/ to/page.html 为页面的实际 URL。 (您不需要它的域,只需要它的路径)

我希望这有帮助。

如果这回答了您的问题,请记得点击旁边的复选标记以接受此答案。

If the file is on the same server/domain, you could just load it in with jQuery. Here is some jQuery code:

<script type="text/javascript">
$(document).ready(function() {
$('#id-of-div').load('/path/to/page.html');
});
</script>

Just change id-of-div to the id of the div that you want the page to be loaded into and change /path/to/page.html to the actual URL to the page. (you don't need the domain of it, just the path to it)

I hope this helps.

If this answers your question, please remember to click the checkmark next to this to accept this answer.

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