Home >Web Front-end >JS Tutorial >Solution to the problem that the iframe of document.createElement in IE cannot set the attribute name_javascript skills

Solution to the problem that the iframe of document.createElement in IE cannot set the attribute name_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:39:312102browse

The name of the iframe can be the target of a link or form. Open the link or form to this iframe.
I encountered an issue before where I couldn’t set the name attribute of an iframe in IE

JavaScript code

var iframe = document.createElement('iframe');  
iframe.name = 'ifr';  
//iframe.setAttribute('name', 'ifr'); //这样也不行 

Neither of the above two methods can be set. Later I found out that it can also be created like this

JavaScript code

var iframe = document.createElement('2049015ba9c73891d46baf35dd342298065276f04003e4622c4fe6b64f465b88');

This is no problem in IE, but this method cannot be passed in Firefox. So finally

JavaScript code

 try{  
   var iframe = document.createElement('<iframe name="ifr"></iframe>');  
  }catch(e){ 
    var iframe = document.createElement('iframe');  
    iframe.name = 'ifr';  
 }

This way it is compatible.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn