开发者

From Which Frame alert occurs?

I have a page with six ifrmaes inside it. Each frame has individual id, so it is easy to detect the frame. All these frames have commono src. And each source I开发者_如何学JAVA set

window.onload=function(){
  alert(' this has been alerted from Iframe with id#");
}

How can I know the ID of the frame from which the alert is alerting?

Thanks.


You can do it like this:

Frameset:

<html>
  <body>
    <iframe src="frame.html" id="frameID1" name="frameName1"></iframe>
    <iframe src="frame.html" id="frameID2" name="frameName2"></iframe>
    <iframe src="frame.html" id="frameID3" name="frameName3"></iframe>
    <iframe src="frame.html" id="frameID4" name="frameName4"></iframe>
    <iframe src="frame.html" id="frameID5" name="frameName5"></iframe>
    <iframe src="frame.html" id="frameID6" name="frameName6"></iframe>
  </body>
</html>

Frame:

<html>
  <head>
    <script type="text/javascript">
      window.onload=function() {
        alert('This has been alerted from frame with id#: ' + GetFrameID(this.name));
      }

      function GetFrameID(frameName) {
        var frames = top.document.getElementsByTagName('iframe');
        if (frames != null) {
          for (var i = 0; i < frames.length; i++) {
            if (frames[i].name == frameName) return frames[i].id;
          }
        }
        return null;
      }
    </script>
  </head>
  <body>
  </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜