javascript check whether a specific element (iframe) exist, if not, appendchild - Stack Overflow

i want to achieve some feature like this: var el = document.createElement("iframe");el.setAt

i want to achieve some feature like this:

var el = document.createElement("iframe");
el.setAttribute('id', 'ifrm');
document.body.appendChild(el);
el.setAttribute('src', '');

and if "ifrm" exists, jt won't create new iframe. if it doesn't exist, it will create that frame. i want to do this to avoid duplicate frame creation when user keeps click on the same button to generate frame. is there a way to check whether a specific iframe exists? thanks

i want to achieve some feature like this:

var el = document.createElement("iframe");
el.setAttribute('id', 'ifrm');
document.body.appendChild(el);
el.setAttribute('src', 'http://www.example.');

and if "ifrm" exists, jt won't create new iframe. if it doesn't exist, it will create that frame. i want to do this to avoid duplicate frame creation when user keeps click on the same button to generate frame. is there a way to check whether a specific iframe exists? thanks

Share Improve this question edited Jun 12, 2012 at 7:21 Tomalak 339k68 gold badges546 silver badges635 bronze badges asked Jun 12, 2012 at 7:19 MarioMario 8854 gold badges19 silver badges30 bronze badges 2
  • document.getElementById() – Tomalak Commented Jun 12, 2012 at 7:21
  • a better solution would be to remove the button entirely, or unbind the click event from the button and disable it when you click on it – Andy Ray Commented Jun 12, 2012 at 7:49
Add a ment  | 

2 Answers 2

Reset to default 2

You can check with getElementById('ifrm') whether or not the frame with that id already exists. If not, create it. Like so:

if(!document.getElementById("ifrm")) 
   // Create the damn thing

If you wish to check if there is any iframe at all, you could use getElementsByTagName('iframe').

To make live a little easier, you can take a look at jQuery. This is a Javascript library that helps you to create DOM objects and/or find them in the DOM tree.

if(document.getElementById("ifrm"))
   //then it exists
else
   //it doesn't exist yet

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信