views:

38

answers:

1

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 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.

+2  A: 

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>
Sani Huttunen
Thank you so much. It worked perfectly. Inever thought that I could call the window by the iframe's name. Thank you once again.
Hoque