admin管理员组文章数量:1416051
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
2 Answers
Reset to default 2You 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
本文标签:
版权声明:本文标题:javascript check whether a specific element (iframe) exist, if not, appendchild - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745247719a2649633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论