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

本文标签: