Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function createElementWithName( type, name ){
var element;
// First try the IE way; if this fails then use the standard way
if( document.all ){
element =
document.createElement( '< '+type+' name="'+name+'" />' );
}else{
element = document.createElement( type );
element.setAttribute( 'name', name );
}
return element;
}
function createElementWithName(){}
(function(){
try {
var el=document.createElement( '<div name="foo">' );
if( 'DIV'!=el.tagName ||
'foo'!=el.name ){
throw 'create element error';
}
createElementWithName = function( tag, name ){
return document.createElement( '<' + tag + ' name="' +
name + '"></' + tag + '>' );
}
}catch( e ){
createElementWithName = function( tag, name ){
var el = document.createElement( tag );
// setAttribute might be better here ?
el.name = name;
return el;
}
}
})();
var createElementWithName = ( function(){
try {
var el = document.createElement( '<div name="foo">' );
if( el.tagName !== 'DIV' || el.name !== 'foo' ){
throw 'create failed';
}
return function( tag, name ){
return document.createElement( '<' + tag + ' name="' +
name + '"></' + tag + '>' );
};
}catch( e ){
return function( tag, name ){
var el = document.createElement( tag );
el.setAttribute( 'name', name );
return el;
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.