2

To get a collection of element we use the getElementsByName() method.Then we can use it to reference individual elements using indexes [0,1,2,...].But i have an example where name attribute is directly used to indicate an iframe element.This is a new thing to me.I don't know what to google to find some articles about it.Can anyone explain to me what it is and under which circumstances i can use this type of reference method.

i mean here mask.document.designMode is directly using the name attribute to reference the ifram.Shouldn't it be document.getElementById('mask').document.designMode ?

My code enables the designmode for the iframe element.It works in my browsers but not sure why not working in SO's editor.

function iframeOn(){
  mask.document.designMode='on';
 
}
<iframe name='mask' id='mask' onload='iframeOn();' style='border:1px solid black;width:700px;height:300px;word-wrap:break-word;padding-left:2px;padding-right:2px;padding-bottom:2px;overflow-y:scroll;' ></iframe>
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • 1) Are you sure its the name attribute and not the ID attribute? 2) Are you running any other code (like libraries) on your page where it's working that might not be coming along for the ride onto SO? If the answer to the 2nd question is "no" then I suggest looking for document.getElementById() – Raydot Jul 28 '15 at 19:34
  • Yes, it's a way to refer `window` objects. Notice, that using `name` will refer to the `window` within the `iframe`, `iframe` element itself (given by `gEBI()`) has not `document` object. – Teemu Jul 28 '15 at 19:34
  • @DaveKaye yup ,i am pretty sure :). it is running on my browser right now. – AL-zami Jul 28 '15 at 19:50
  • @Teemu you seems to understand my question.can you elaborate it ? or post an well explained answer ?still not clear to me – AL-zami Jul 28 '15 at 19:53
  • @AL-zami I tried to find some good documentation about use of the `name`, but no luck so far. Anyway, I'd recall I've read, in context of `name` attribute was declared deprecated, something like it would still be valid when using with `window` objects and `form` elements and few other tags only. This was a couple of years ago, things might have been changed after that. A handy way would be to use [`frames`](https://developer.mozilla.org/en-US/docs/Web/API/Window/frames) collection. – Teemu Jul 28 '15 at 20:03
  • @AL-zami Well, at least [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Element/name) lists the elements you can apply `name` attribute ... – Teemu Jul 28 '15 at 20:15
  • @AL-zami so then the 2nd part of my comment applies, as mentioned in the answer just below. – Raydot Jul 28 '15 at 20:28

1 Answers1

2

Shouldn't it be document.getElementById('mask').document.designMode

No. it should be

 var iframe=document.getElementById("mask");
 var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
    iframeDocument .designMode....

getElementById('mask') gives you an Iframe element which you need its window/contentWindow object which has document which now you can do whatever you want.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • thanks ! i understand the gEBI() part.But what about using the name directly? – AL-zami Jul 28 '15 at 19:55
  • 1
    @AL-zami Don't use it via name. use it strictly as you should. I've seen this when acceesing a form attributes. but it's not safe. since there might be overrides. read [here](http://stackoverflow.com/questions/1397592/difference-between-id-and-name-attributes-in-html) – Royi Namir Jul 28 '15 at 19:59
  • This is what I was saying to you in my comment above. It's the ID not the name that you're working with here. And yes, don't work with name. It can change context, and there are other reasons. – Raydot Jul 28 '15 at 20:27