Il problema con il sopra soluzione è che può essere rimosso mediante un semplice:
if(top != self)
delete top.onbeforeunload;
una volta che è stato chiamato, allora il prevent_bust
sarà n mai essere incrementato, e ciò significa che il sito web sarà liberamente reindirizzato senza il tuo consenso o conoscenza. Cattivo affare
Se si voleva una versione costantemente funzionale di questa soluzione, mi sento di raccomandare di fare questo, invece:
// Create a random seed value, making it almost impossible to
// determine what is being tested for.
var prevent_bust = Math.random() * 3000;
// enclose everything in a function, so that it cannot be addressed
function iniFunc (init) {
// The function is no longer in scope of the main window.
function onbeforeunload() { prevent_bust++ }
window.onbeforeunload = onbeforeunload;
setInterval(function() {
// make sure the function was not deleted.
if(window.onbeforeunload != onbeforeunload)
{
prevent_bust = init + 1;
window.onbeforeunload = onbeforeunload;
}
if (prevent_bust > init) { // All comparison is to the random seed.
prevent_bust -= 2
window.top.location = 'http://server-which-responds-with-204.com/'
// Unfortunately, you have absolutely no idea which website caused
// the incrementation, so you cannot replace it with a link!
//
// You might try to simply ignore it and just use the iframe as is --
// theoretically, they are no longer able to bust this frame.
// (this theory will be disproved below).
}
}, 1);
};
iniFunc(prevent_bust);
Purtroppo, questo provoca lasciare un problema - è una cosa banale per recuperare l'intervallo che ha stato impostato, non impostato, e quindi la pagina reindirizzare:
// setTimeout will return the highest timeout which is not "in use", in this case,
// it will be the original setInterval (from the above function) + 1.
// Event if there are 1,000 intervals already set, it will be rather trivial to
// clear them all.
var currentInterval = 10000;
// window.setTimeout(gotoHREF, 100);
// clearInterval will not interfere with setTimeout, so we can clear all
// of the Intervals already set.
for(var i = 0; i < currentInterval; i++) top.clearInterval(i);
function gotoHREF(){
top.location.href = "http://<my-url/>";
}
la cosa migliore è in realtà per risolvere questo problema di server lato (se potete). Se si ha accesso al server per il sito web che conterrà iframe, creare un interim, posizione procura in cui si tira nei dati del sito e quindi a nudo i tag di script:
// In php
$dd = new DOMDocument();
// file_get_contents will simply convert the entire web address into a String
$dd->loadXML(file_get_contents("http://" . $_GET[ 'loadedURL' ]));
$scripts = $dd->getElementsByTagName("script");
// iterate through the website and remove all script tags.
for($i = 0; $i < $scripts->length; $i++)
{
$current = $scripts->item($i);
$current->parentNode->removeChild($current);
}
// output it to the dummy page.
echo $dd->saveXML();
Si potrebbe quindi utilizzare il tag:
<iframe src="redirect.php?loadedURL=http://www.google.com"></iframe>
Sfortunatamente, ciò significa che l'iframe verrà eseguito senza JavaScript che potrebbe paralizzare, se non completamente, lobotomizzare il sito Web in questione. Dovrai anche assicurarti che tutti gli attributi src siano correttamente modificati per i nodi discendenti nel codice HTML del sito straniero.
D'altra parte, è possibile che il server controlli il sito con frame e tutte le relative pagine JS per vedere se la posizione superiore di RegExp (. | [\ S * ("| ')) (corrisponde alla parte superiore. location, top ["location, and top ['location] esiste (o se ci sono riferimenti a top in assoluto), e quindi usa un link se esiste e il sito corretto se non lo è. Il danno è che quindi stai costringendo l'utente ad aspettare che il sito secondario si carichi due volte, una volta sul server e una volta nel browser. (a meno che tutto venga fatto tramite JS, ma a mio avviso è generalmente più fastidioso).
Personalmente, sono dell'opinione che la folla "non inquadrare il mio sito" possa generalmente vincere la maggior parte delle battaglie che coinvolgono direttamente l'iframe.D'altra parte, se il codice viene utilizzato per elaborare l'HTML prima di essere aggiunto a una pagina Web, l'altra faccia è più di una possibilità di combattimento.
Come nota laterale, tutti questi può essere realizzato tramite JavaScript e AJAX, ma sarà generalmente un po 'più lento. Usa il server, se puoi.
La prima opzione sembra buona. C'è un problema però, un refresh premuto sul browser attiverà il gestore window.onbeforeunload ed eseguirà esattamente la stessa azione definita nella funzione. Inoltre, qualsiasi azione sul sito, come una richiesta di pagina o un invio di un modulo, non viene eseguita mentre il gestore viene richiamato di nuovo. La soluzione per questo sarebbe un po 'doloroso "Aggiungi pre-invio/navigazione javascript per annullare le azioni dallo script frame buster. –
Questo non impedisce a qualcuno di usare'
.. ' – Andrew