Domanda

Ho problemi a chiamare una funzione JavaScript in un iframe dalla pagina principale. Ecco le mie due pagine:

mainPage.html

<html>
<head>
    <title>MainPage</title>
    <script type="text/javascript">
        function Reset() 
        {
            if (document.all.resultFrame)
                alert("resultFrame found");
            else
                alert("resultFrame NOT found");

            if (typeof (document.all.resultFrame.Reset) == "function")
                document.all.resultFrame.Reset();
            else
                alert("resultFrame.Reset NOT found");
        }
    </script>
</head>
<body>
    MainPage<br>
    <input type="button" onclick="Reset()" value="Reset"><br><br>
    <iframe height="100" id="resultFrame" src="resultFrame.html"></iframe>
</body>
</html>

resultFrame.html

<html>
<head>
    <title>ResultPage</title>
    <script type="text/javascript">
        function Reset() 
        {
            alert("reset (in resultframe)");
        }
    </script>
</head>
<body>
    ResultPage
</body>
</html>

(So che document.all non è raccomandato ma questa pagina dovrebbe essere visualizzata solo con IE internamente e non credo che sia il problema)

Quando premo il pulsante Reset ottengo " resultFrame trovato " e " resultFrame.Reset NON trovato " ;. Sembra avere un riferimento al frame ma non riesco a chiamare la funzione sul frame, perché?

È stato utile?

Soluzione

Usa:

document.getElementById("resultFrame").contentWindow.Reset();

per accedere alla funzione di ripristino nell'iframe

document.getElementById (" resultFrame ") otterrà l'iframe nel tuo codice e contentWindow otterrà l'oggetto finestra nell'iframe. Una volta che hai la finestra figlio, puoi fare riferimento a JavaScript in quel contesto.

Vedi anche QUI in particolare la risposta di bobince.

Altri suggerimenti

Invece di ottenere la cornice dal documento, prova a ottenere la cornice dall'oggetto finestra.

nell'esempio sopra cambia questo:

if (typeof (document.all.resultFrame.Reset) == "function")
    document.all.resultFrame.Reset();
else
    alert("resultFrame.Reset NOT found");

a

if (typeof (window.frames[0].Reset) == "function")
    window.frames[0].Reset();
else
    alert("resultFrame.Reset NOT found");

il problema è che l'ambito del javascript all'interno dell'iframe non è esposto attraverso l'elemento DOM per l'iframe. solo gli oggetti finestra contengono le informazioni di scoping javascript per i frame.

Per una resistenza ancora maggiore:

function getIframeWindow(iframe_object) {
  var doc;

  if (iframe_object.contentWindow) {
    return iframe_object.contentWindow;
  }

  if (iframe_object.window) {
    return iframe_object.window;
  } 

  if (!doc && iframe_object.contentDocument) {
    doc = iframe_object.contentDocument;
  } 

  if (!doc && iframe_object.document) {
    doc = iframe_object.document;
  }

  if (doc && doc.defaultView) {
   return doc.defaultView;
  }

  if (doc && doc.parentWindow) {
    return doc.parentWindow;
  }

  return undefined;
}

e

...
var el = document.getElementById('targetFrame');

var frame_win = getIframeWindow(el);

if (frame_win) {
  frame_win.reset();
  ...
}
...

Chiama

window.frames['resultFrame'].Reset();

objectframe.contentWindow.Reset () devi prima fare riferimento all'elemento di livello superiore nel frame.

La prima e principale condizione che deve essere soddisfatta è che sia il genitore che l'iframe debbano appartenere alla stessa origine. Una volta fatto ciò, il figlio può invocare il genitore usando il metodo window.opener e il genitore può fare lo stesso per il figlio di cui sopra

Quando accedi a resultFrame tramite document.all, lo estrae solo come elemento HTML, non come cornice di una finestra. Si ottiene lo stesso problema se si verifica un frame frame di un evento utilizzando un " questo " autoreferenzialità.

Sostituire:

document.all.resultFrame.Reset();

Con:

window.frames.resultFrame.Reset();

o

document.all.resultFrame.contentWindow.Reset();

Se non puoi usarlo direttamente e se riscontri questo errore: Bloccato un frame con origine " http: // www ..com " dall'accesso a una cornice di origine incrociata. Puoi utilizzare postMessage () invece di utilizzare il funzione direttamente.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top