我应该使用 iframe 而不是 ajax 吗? [英] Should I use an iframe rather than ajax?

查看:32
本文介绍了我应该使用 iframe 而不是 ajax 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在构建一个带有音频播放器的 Drupal 网站,该网站在您浏览页面时应继续播放(类似于 soundcloud).

We're building a Drupal site with an audio player which should keep playing while you browse the page (something like soundcloud).

目前,我们正在使用 AJAX 加载点击的 站点,并将 HTML 注入页面并使用 history.js 来操作位置.

Currently we're loading clicked <a>'s sites with AJAX and inject the HTML into the page and use history.js to manipulate the location.

虽然有一个主要的缺点:

There is a major downside though:

由于新加载的页面上可能有不同的 CSS 和 JS,我们必须将当前的脚本、样式和链接元素与 AJAX 响应文本(丑陋的regex)进行比较,然后只添加新的,但不存在的元素.通过这样做,一旦执行脚本,加载另一个页面将不会删除/删除/撤消.这可能会成为一个性能问题(而 regex 无论如何都是针对标记的).

Since a newly loaded page may have different CSS and JS on it, we have to compare the current script, style and link elements to the AJAX response text (ugly regex) and only add the new, yet non-existing elements. By doing so, loading another page won't delete/remove/undo once executed scripts. This may become a performance issue (while regex against markup is anyways).

使用 iframe 可以解决这个问题和其他问题:

Using an iframe may solve this and other problems:

当您第一次点击网站上的链接时,页面内容会被一个 iframe 替换,它只是加载页面.音频播放器位于原始 DOM 中,即在 iframe 之外.点击 iframe 内的链接也可以在历史 API 中捕获和使用.

When you first click a link on the site, the page content is replaced by an iframe which just loads the page. The audio player is in the original DOM, i.e. outside the iframe. Clicking links inside the iframe can be caught and used within the history API as well.

所以,虽然我从没想过我会这么认为:我认为使用 iframe 可能是这里更好的选择,不是吗?

So, although I've never thought I'd think so: I think using an iframe may be the better option here, isn't it?

PS:我的主要问题是我无法控制网站上不同页面的脚本不同,样式确实没问题.如果整个网站只有一个巨大的静态 JS 和 CSS,我就没有问题.

PS: My main problem is that I can't control that different pages on the website have different scripts, the styles really are no problem. If there was just one huge static JS and CSS website-wide, I didn't have a problem.

更新

iframe 解决方案工作正常,但在 iOS 中除外.Iframes 有很多问题.不过,我认为我们将采用 iframe 解决方案.

The iframe solution works fine, except in iOS. Iframes are buggy as hell. Still, I think we're going with the iframe solution.

推荐答案

我会说...保持简单,愚蠢.选择 iframe 解决方案,我知道它会扼杀你的一部分灵魂,但它是解决问题最简单的方法,代表了未来遇到问题的最低变化.

I would say... keep it simple, stupid. Go for the iframe solution, I know it kills a part of your soul, but it is the easiest solution to your problem and represents the lowest change of running into problems in the future.

亲吻.

这篇关于我应该使用 iframe 而不是 ajax 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆