Skip to content

Test: old-style IE createElement #258

Closed
jamesarosen opened this Issue May 11, 2011 · 3 comments

2 participants

@jamesarosen

In IE < 9, the following is legal:

var e = document.createElement("<input name='foo'></div>");

Indeed, it's the only way to create an element with a name property on older versions of IE.

In Firefox, Chrome, Safari, Opera, and IE 9, it's not. The proper way to do that is:

var e = document.createelement('input');
e.setAttribute('name', 'foo');

I'd love to have a test for old-style IE createElement support:

Modernizr.addTest('htmlCreateElement', function() {
  var result;
  try {
    document.createElement("<input name='test' />");
    result = true;
  } catch(e) {
    result = false;
  }
  return result;
});
@paulirish
Modernizr member

how about

document.createElement("<input name='test' />");
result = true;

changes to

return document.createElement("<input name='test' />").getAttribute('name') == 'test';
@jamesarosen

The problem is that document.createElement("<input name='test' />") will throw an exception on everything except IE6-8. We could do

try {
  return document.createElement("<input name='test' />").getAttribute('name') == 'test';
} catch(e) {
  return false;
}
@paulirish
Modernizr member

yup! that's what i meant. :)

@paulirish paulirish closed this in 13d4566 May 21, 2011
@patrickkettner patrickkettner pushed a commit to patrickkettner/Modernizr that referenced this issue Feb 22, 2015
@paulirish paulirish `<input name='test' />` test. fixes #258 cb02032
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.