get the id of the image from another html file using javascript - Stack Overflow

I using iframe for displaying a image. Here is my code.index.html<html><head><title>C

I using iframe for displaying a image. Here is my code.

index.html

<html>
 <head>
    <title>Center</title
    <script type="text/javascript">
            function sample(id){
               var frameht=document.getElementById("ifrm").style.height;
               var framewd=document.getElementById("ifrm").style.width;
           }
    </script>
 </head>
 <body>
    <iframe style="width:700px;height:600px;" src="test.html" id="ifrm" onload="javascript:sample(this.id);"></iframe>
 </body>
</html>

test.html

<html>
<head>
    <title>Center</title>
    <script type="text/javascript">

    </script>
</head>
<body>
    <img src="14.jpg" id="img1" onload="javascript:sample();"/>
</body>
</html>

I want the id of the image in index.html. Is it possible to get? Any one can help? Please!

I using iframe for displaying a image. Here is my code.

index.html

<html>
 <head>
    <title>Center</title
    <script type="text/javascript">
            function sample(id){
               var frameht=document.getElementById("ifrm").style.height;
               var framewd=document.getElementById("ifrm").style.width;
           }
    </script>
 </head>
 <body>
    <iframe style="width:700px;height:600px;" src="test.html" id="ifrm" onload="javascript:sample(this.id);"></iframe>
 </body>
</html>

test.html

<html>
<head>
    <title>Center</title>
    <script type="text/javascript">

    </script>
</head>
<body>
    <img src="14.jpg" id="img1" onload="javascript:sample();"/>
</body>
</html>

I want the id of the image in index.html. Is it possible to get? Any one can help? Please!

Share Improve this question asked Feb 14, 2013 at 10:48 ijarlaxijarlax 5152 gold badges12 silver badges18 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 2
document.getElementById('ifrm').contentWindow.document.getElementById('img1');

this should work

img = document.getElementById('ifrm').contentWindow.document.getElementsByTagName('img'); 

img[0].id to get the dinamically id

First get iframe.

var iframe = document.getElementById('ifrm');

Get access to elements of iframe.

var frameDoc = iframe.contentDocument || iframe.contentWindow.document;

Then, get id by tagname. In our case img.

var el = frameDoc.getElementsByTagName('img')[0].id;

Here is an example without iframe, to give you an idea how to get id of element after: http://jsfiddle/nukec/eymq2/

Insted of id use name and try this..

<script>
window.frames["fName"].document.getElementById("img1");
</script>

<iframe style="width:700px;height:600px;" src="test.htm" id="ifrm" name="fName" onload="javascript:sample(this.id);"></iframe>

You should use jQuery

var imageObj= $('#myiframe').contents().find('img');
var imgId=$(imageObj).attr("id");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745479495a4629500.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信