Excepción de DOM de Carácter no válido en IE9


La siguiente pieza de JS que solía funcionar en IE8 está fallando ahora en IE9.

document.createElement('<iframe id="yui-history-iframe" src="../../images/defaults/transparent-pixel.gif" style="position:absolute;top:0;left:0;width:1px;height:1px;visibility:hidden;"></iframe>');

Obtengo la siguiente excepción: SCRIPT5022: Excepción DOM: INVALID_CHARACTER_ERR (5)

Es el fragmento de código anterior que no está de acuerdo con los estándares. ¿Cuál es la solución para el problema?

Author: Josh Unger, 2011-03-17

6 answers

La API de createElement especifica que el constructor quiere un string que especifique el nombre de un elemento. Parece que IE9 es más, siguiendo estrictamente las normas. Puede lograr lo mismo que está tratando de hacer con el siguiente código:

var iframe = document.createElement("iframe");
iframe.setAttribute("id", "yui-history-iframe");
iframe.setAttribute("src", "../../images/defaults/transparent-pixel.gif");
iframe.setAttribute("style", "position:absolute;top:0;left:0;width:1px;height:1px;visibility:hidden;");

Http://msdn.microsoft.com/en-us/library/ms536389 (v=vs.85). aspx

 29
Author: Adam Ayres,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-17 19:24:29

Para jQuery.bgiframe.js, sería una mejor solución para arreglar la prueba IE6 incorrecta?

¿Qué hay de reemplazar esto:

if($.browser.msie&&/6.0/.test(navigator.userAgent)

Con algo como esto:

if ($.browser.msie && $.browser.version=="6.0")
 13
Author: Marco,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-12-22 17:35:06

Para jquery.bgiframe.js:

He descargado la versión 1.1.3 pre en

Https://github.com/brandonaaron/bgiframe/blob/master/jquery.bgiframe.js

Y esto resolvió el problema.

 7
Author: Tillito,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-21 13:48:26

Este error puede ocurrir cuando accidentalmente se usan nombres de funciones estándar de JavaScript como sus propios nombres de funciones si no utiliza espacios de nombres. Por ejemplo, tengo un concepto llamado "Atributo" y quería probar una nueva función que creara una nueva de esas:

<button onclick="createAttribute('Pony')">Foo</button>
<button onclick="createAttribute('Magical pony')">Bar</button>
<script type="text/javascript">
    function createAttribute(name) { alert(name); } 
</script>
  • Hacer clic en "Foo" no te da nada
  • Hacer clic en Foo te da INVALID_CHARACTER_ERR (5) o InvalidCharacterError: DOM Exception 5
  • Abrir tu consola de desarrollo y ejecutar createAttribute('Django') te da la alerta

Lo que está sucediendo es que los botones son llamando document.createAttribute() y tu consola de desarrollo está llamando a la función que declaraste.

Solución: use un nombre de función diferente o mejor aún, un espacio de nombres.

 0
Author: Nenotlep,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-12 08:17:52

Si está dispuesto a sacrificar un poco de rendimiento y puede usar una biblioteca externa, le sugeriría usar:

Prototipo JS

var el = new Element('iframe', {
  id: "<your id here>",
  src: "<your source here>",
  style: "<your style here>"
});

JQuery

var el = jQuery('<iframe>', {
  id: '<your id here>',
  src: "<your source here>",
  style: "<your style here>"
})[0];

Esto se encarga de todas las inconsistencias entre navegadores y también es mucho más bonito: -)

Nota: Este es un pseudocódigo no probado-consulte las páginas de documentación oficial de Prototype JS y jQuery para obtener más información.

 0
Author: Niko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-29 09:05:12

Esta es una solución para jquery de .bgiframe.js .

if ( $.browser.msie && /9.0/.test(navigator.userAgent) ) {
                var iframe = document.createElement("iframe");
                iframe.setAttribute("class", "bgiframe");
                iframe.setAttribute("frameborder", "0");
                iframe.setAttribute("style", "display:block;position:absolute;z-index:-1;filter:Alpha(Opacity=\'0\');top:expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\');left:expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\');width:expression(this.parentNode.offsetWidth+\'px\');height:expression(this.parentNode.offsetHeight+\'px\');");
                this.insertBefore( iframe, this.firstChild );
            } else {
                this.insertBefore( document.createElement(html), this.firstChild );
            }
 -6
Author: kees0000,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-11 19:58:35