¿Por qué no Function.apply () trabajar más allá de los límites del documento en el IE?

StackOverflow https://stackoverflow.com/questions/1104265

Pregunta

Estoy viendo un comportamiento extraño en el IE tratando de llamar a funciones en otra página a través de Function.apply ().

Esto es una prueba sencilla:

test1.html:

<HTML>
<HEAD>
<script language="javascript" type="text/javascript">
  var opened = null;

  function applyNone() {
    opened.testFunc.apply(opened);
  }

  function applyArgs() {
    opened.testFunc.apply(opened, ["applied array"]);
  }

  function call() {
    opened.testFunc("called directly");
  }

  function remoteApply() {
    opened.testApply(["used remote apply"]);
  }

  function remoteApplyCopy() {
    opened.testApplyCopy(["used remote apply copy"]);
  }

  function openPopup() {
    opened = window.open("test2.html", "_blank");
  }
</script>
</HEAD>
<BODY>
  <a href="#" onclick="openPopup()">OPEN</a>
  <hr>
  <a href="#" onclick="applyNone()">applyNone</a>
  <a href="#" onclick="applyArgs()">applyArgs</a>
  <a href="#" onclick="call()">call</a>
  <a href="#" onclick="remoteApply()">remoteApply</a>
  <a href="#" onclick="remoteApplyCopy()">remoteApplyCopy</a>
</BODY>
</HTML>

Test2.html:

<HTML>
<HEAD>
<script language="javascript" type="text/javascript">
  function testApply(args) {
    testFunc.apply(this, args);
  }

  function testApplyCopy(args) {
    var a = [];
    for(var i = 0; i < args.length; i++) {
      a.push(args[i]);
    }
    testFunc.apply(this, a);
  }

  function testFunc() {
    var s = "Got: ";
    for(var i = 0; i < arguments.length; i++) {
      s += arguments[i] + " ";
    }
    document.getElementById("output").innerHTML += s + "<BR>";
  }
</script>
</HEAD>
<BODY>
  Hi there
  <div id="output"/>
</BODY>
</HTML>

En Firefox y Chrome todos los métodos funcionan correctamente.

En IE (probado en 6, 7, y 8) todos, pero los applyArgs () y remoteApply () métodos funcionan como se esperaba.

applyArgs () da un "objeto JScript espera" error cuando intenta llamar a aplicar (test1.html línea 11).

remoteApply () da el mismo "objeto JScript espera" error cuando intenta llamar a aplicar (Test2.html línea 5).

El problema es que necesito para poder utilizar apply (). Puedo conseguir evitar el problema de hacer algo como el mecanismo remoteApplyCopy (), pero estoy tratando de evitar eso. ¿Por qué no se aplica () sólo el trabajo?

¿Fue útil?

Solución

Es necesario tener las matrices creadas en la otra ventana, ya que cada ventana tiene su propio constructor de Array. Creo que esto va a funcionar.

Añadir esta función a Test2.html:

function getEmptyArray() {
    return new Array();
}

Y esta función para Test1.html:

Array.prototype.cloneToRemote = function (win) {
    var newArray = win.getEmptyArray();
    for (var i = 0; i < this.length; i++)
    {
        newArray.push(this[i]);
    }
    return newArray;
}

A continuación, haga lo siguiente:

function applyArgs() {
    opened.testFunc.apply(opened, ["applied array"].cloneToRemote(opened));
}

Nota, parece que usted debería ser capaz de hacer

var newArray = new win.Array();

dentro de la función test1.html cloneToRemote (), pero no podía hacer ese trabajo. Si se pudiera hacer eso, usted podría deshacerse de la función nueva getEmptyArray () en Test2.html.

Otros consejos

No tengo idea de por qué esto funciona, pero yo estaba jugando con su código y topé con una solución ... puso funciones de test2 interior de test1 y funciona:

<HTML>
<HEAD>
<script language="javascript" type="text/javascript">
  var opened = null;

  function applyArgs() {
    testFunc.apply(opened, ["applied array"]);
  }

  function openPopup() {
    opened = window.open("test2.html", "_blank");
  }

  function testFunc() {
    var s = "Got: ";
    for(var i = 0; i < arguments.length; i++) {
      s += arguments[i] + " ";
    }
    this.document.getElementById("output").innerHTML += s + "<BR>";
  }
</script>
</HEAD>
<BODY>
  <a href="#" onclick="openPopup()">OPEN</a>
  <hr>
  <a href="#" onclick="applyArgs()">applyArgs</a>
</BODY>
</HTML>

Me haré saber si puedo averiguar más (IE es raro por el estilo). Como he dicho, sólo estaba jugando con el código.

Si cambia la función Test2.html testApply () de la siguiente manera:

function testApply() {
    testFunc.apply(this, arguments);
}

remoteApply () funciona. Pero, applyArgs () todavía fallaron.

" ... applyArgs () da un "objeto JScript espera" error cuando intenta llamar a aplicar (línea test1.html 11). remoteApply () da el mismo "objeto JScript espera" error cuando intenta llamar a aplicar (Test2.html línea 5). ... "

¿Qué objeto exacto no es "objeto de JScript" como "espera"?

(pista: uso depurador)

- DBJ

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top