Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aarongustafson
Created July 25, 2010 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarongustafson/489679 to your computer and use it in GitHub Desktop.
Save aarongustafson/489679 to your computer and use it in GitHub Desktop.
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