Profil de Stephen喜欢天空的蓝色PhotosBlogListesPlus Outils Aide

Blog


28 octobre

多页面共享子窗口

这是来自论坛上的一个问题,问题要求多个不同的页面,在打开子页面时,要共用同一个窗口,并且子窗口不能刷新。这跟google音乐里面的添加音乐,然后播放的效果类似。因为在播放音乐的时候,如果子页面刷新了,那么音乐必须从头来播放,自然效果不尽人意。类似的情况也会出现在视频的网站上。
下面,我们使用简单的Javascript代码来实现这一功能。本代码在目前所有主流浏览器里测试通过。
下面,先创建一个测试主页面,Test1.htm
XML/XHTML 代码
<!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">
<head>
  <title></title>
  <script type="text/javascript">
    function winOpen() {
      window.open("winA.htm?music=From_Test1_" + (new Date()).toLocaleString(), "mxh", "width=600,height=300")
    }
  </script>
</head>
<body>
  <input type="button" onclick="winOpen()" value="打开子窗口" />
</body>
</html>
接下来,创建WinA.htm
XML/XHTML 代码
<!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">
<head>
  <title>Window A</title>
</head>
<body>
<h1>这里是Window A</h1>
<script type="text/javascript">
  if (window.location.href == window.top.location.href) {
    self.top.location.href = "winB.htm" + window.location.search
  }
  else {
    window.parent.frames["mxh2"].setMusic(window.location.search.substr(1))
  }
</script>
</body>
</html>
接下来是WinB.htm
XML/XHTML 代码
<!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">
<head>
    <title>Window B</title>
</head>
<body>
<iframe name="mxh2" src="winC.htm" style="width:600px;height:400px;" frameborder="0"></iframe>
<iframe name="mxh" src="winD.htm" style='display:none'></iframe>
<script type="text/javascript">
  alert("页面刷新检测器")
</script>
</body>
</html>
WinC.htm
XML/XHTML 代码
<!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">
<head>
  <title>Window C</title>
</head>
<body>
<div id="showContent">初始化的内容</div>
    <script type="text/javascript">
      function setMusic(str) {
        document.getElementById("showContent").innerHTML += "<br/>" + str
      }
    </script>
</body>
</html>
WinD.htm
XML/XHTML 代码
<!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">
<head>
  <title>Window D</title>
</head>
<body>
  <script type="text/javascript">
    window.name = "mxh";
    window.top.name = "";
    window.top.focus();
  </script>
</body>
</html>
然后可以创建多个测试用的页面Test2.htm
XML/XHTML 代码
<!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">
<head>
  <title></title>
  <script type="text/javascript">
    function winOpen() {
      window.open("winA.htm?music=From_Test2_" + (new Date()).toLocaleString(), "mxh", "width=600,height=300")
    }
  </script>
</head>
<body>
  <input type="button" onclick="winOpen()" value="打开子窗口" />
</body>
</html>
在不同的窗口里,打开测试页面,则,都会将内容显示在Window B所在的主体窗口内。并且页面是不刷新的。其原理就是将新开的页面在隐藏的窗口里打开,然后取出来传递的参数,使用javascript来进行数据的传递。其中窗口WinD.htm中的脚本不能少,而且是很关键的地方。
 
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/net_lover/archive/2009/10/27/4735224.aspx

Commentaires

Veuillez patienter...
Le commentaire entré est trop long. Raccourcissez-le.
Vous n'avez rien entré. Réessayez.
Il est actuellement impossible d'ajouter votre commentaire. Réessayez plus tard.
Pour ajouter un commentaire, tu dois avoir l'autorisation de tes parents. Demander l'autorisation
Tes parents ont désactivé les commentaires.
Il est actuellement impossible de supprimer votre commentaire. Réessayez plus tard.
Vous avez dépassé le nombre maximal de commentaires qu'il est possible d'envoyer le même jour. Réessayez dans 24 heures.
Votre compte a pu laisser les commentaires désactivés parce que nos systèmes indiquent que vous risquez d'arroser d'autres utilisateurs de messages. Si vous pensez que votre compte a été désactivé par erreur, contactez l'assistance en ligne de Windows Live.
Effectuez la vérification de sécurité ci-dessous pour finaliser l'envoi de votre commentaire.
Les caractères entrés pour la vérification de sécurité doivent correspondre à ceux de l'image ou du fichier audio.

Pour ajouter un commentaire, connectez-vous avec votre identifiant Windows Live ID (si vous utilisez Messenger ou Xbox LIVE, vous avez un identifiant Windows Live ID). Connectez-vous


Vous n'avez pas d'identifiant Windows Live ID ? Inscrivez-vous

Rétroliens

L'URL de rétrolien de ce billet est :
http://520life.spaces.live.com/blog/cns!349C9D54C7E61013!201.trak
Blogs Web qui font référence à ce billet
  • Aucune