javascript - 让 <iframe> 异步工作

标签 javascript jquery iframe mp3

是否有一篇好文章或如何让 iframe 或框架与每个页面异步工作?我有一个底部/固定的 div 包裹在 jquery 中,可以在包含 mp3 播放器的悬停时向上滑动。我用 iframe 引用了播放器。我渲染得很好,但是如果不在页面刷新或导航到另一个页面时重新加载,它怎么能继续播放呢?我希望它固定在每个页面的底部并且不刷新地连续播放。我尝试将 iframe 放在每个页面中,但仍然没有用。有任何想法吗?谢谢。

最佳答案

如果它必须留在浏览器中(不是下载应用程序或在音乐/视频播放器中读取流),唯一的方法应该是不要真正更改页面,并加载必须使用 ajax 或 javascript 更改的内容(或者把它放在 (i)frame 中。

但是如果只用讲师创建一个页面并在您的网站上放置一个链接以在另一个选项卡中打开它会容易得多:

<a href="/music-player.htm" target="musicPlayer">Text or what you want</a>

编辑:

所以使用 javascript 会得到与 ajax 相同的结果,但这意味着不更改页面,因此对于 SEO 如果它有点重要则不好。

我所说的 javascript 的意思是,例如,如果您单击链接“主页”,只需动态添加一个 <script type="text/javascript" src="/homepage.js"></script>修改页面内容(同时保留 mp3 播放器)。

否则,如果播放器可以通过 javascript 知道的话,也许可以使用 cookie:

  • 知道播放器是哪个 mp3 文件
  • 此时正在播放 mp3 的播放器是
  • 转到指定的 mp3 文件
  • 在指定时间播放 mp3
  • (如果可以暂停播放器,则应该能够知道播放器是否正在播放)

有可能在更改页面时获得合适的时间(但会有时间加载页面和没有音乐的 mp3 播放器)。

或者可以有 mp3 播放器,它可以节省我们现在的时间,并在此时开始另一个页面(但仍然在切换页面时几秒钟没有声音)。

使用这些方法也会有打开多个页面的问题。

编辑:

我在 iframe 中尝试了页面内容的方式,它工作得很好,但需要 membre 切换到 mp3 模式。

这是 mp3.html(放在根文件夹中,如果不可能,则需要进行一些更改):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>MP3 Player</title>
  <style type="text/css">
        html { 
        font-size: 100%; 
        } 
        body { 
            margin: 0; 
            padding: 0em;
        }
        #frame { width: 100%; height: 100%; border: none; }
        #player { position: absolute; right: 20px; bottom: 20px; }
    </style>
    <script type="text/javascript">
        if ("onhashchange" in window) {
            var h='';
            var command= false;
        window.onhashchange = function(){
                if(location.hash==h) return;
                command= true;
                window.frames['frame'].location.replace(location.hash.replace(/^#/,""));
                h= window.location.hash;
            }
        }
    </script>
</head>
<body>
    <iframe id="frame" onLoad="if(this.src=='')return;if(command)command=!1;else window.location.replace(h='#'+window.frames['frame'].location.href.replace(new RegExp('^'+document.location.origin),''));document.title=window.frames['frame'].document.title;"></iframe>
    <script type="text/javascript">
        document.getElementById("frame").src=document.location.hash.replace(/^#/,"");
    </script>
    <div id="player">
        <object type="application/x-shockwave-flash" data="http://s301826463.onlinehome.fr/so/dewplayer.swf?mp3=http://s301826463.onlinehome.fr/so/Tokyo.mp3" width="200" height="20" id="dewplayer"><param name="wmode" value="transparent"><param name="movie" value="http://s301826463.onlinehome.fr/so/dewplayer.swf?mp3=http://s301826463.onlinehome.fr/so/Tokyo.mp3"></object>
        <a href="javascript:document.location.href=document.location.hash.replace(/^#/,'')">remove mp3 player</a>
    </div>  
</body>
</html>

要在 iframe 和 mp3 播放器中放置一个打开当前页面的链接,它只需要一个链接:

<a href="javascript:parent.location.href='/mp3.html#'+document.location.href.replace(new RegExp('^'+document.location.origin),'')">add mp3 player</a>

一个使用 here 的例子.

关于javascript - 让 &lt;iframe&gt; 异步工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6802898/

相关文章:

javascript - 如何使用 javascript 获取 http 状态代码?

php - Jquery 使网页的某些部分变灰

jQuery css toggle - 需要添加淡入淡出动画过渡

javascript - 跨域iframe内容加载检测

javascript - 检查 JS 是否可以访问 iframe 的文档

javascript - 表格单元格中 iframe 底部的奇怪 "padding"效果

javascript - node.js 非阻塞 mongodb 调用

javascript - 如何测试两个 NodeLists 的相等性?

javascript - 使用 JavaScript 从 iframe 读取数据

jquery - 使 jQuery Mobile 仅适用于菜单而不适用于整个网站