开发者

Find calling iframe id

I have a number of iframes calling a function in my main page. Is there a way to find out the ID of 开发者_运维知识库the iframe which called the function?

There are all part of same domain so that should not be an issue.

Thank you for your time.


The most common way is to have the control pass itself to the function

<script>function doClick(e){ alert('id = ' + e.id); }</script>
<span id=test onclick="doClick(this)"> click here</span>

Another way would be by using the:

event.srcElement

As documented here.

A third way but not as reliable would be by using:

document.activeElement.name

The latter is not the best approach.

In my personal opinion I'd go with the first one, as it will work 100% of the time, and won't give you browser issues.

UPDATE To get the current iframe's id (assuming you don't have nested iframes on the same page), you can simply use:

this.document.getElementsByTagName('body')[0]​.id

And then simply pass it along with the call to your function. just make sure the body has the same ID as your iframe for example.

Let me know if it helps you.


I'd say you need to send an identifier along with your method calls. It can be an optional parameter that does no harm if not present, like:

function foo(value, refId) {
  // do stuff with value
  if (typeof refId!=='undefined') {
    // handle this
  }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜