Question

J'ai créé une fenêtre contextuelle et je voudrais utiliser une fonction que j'ai créée dans la fenêtre parent. J'ai essayé d'utiliser window.oopener.parentFunction () mais pour savoir disponible. Quelqu'un d'autre a-t-il rencontré ce problème? Voici à quoi ressemble mon code.

function parentFunction(){
alert('testing');
}


function print(){
var new_win = window.open('','name','height=400,width=500');
var output = "<html><head></head><body><table>\n";


for (i = 0; i < 10; i++) {
output += "<td onclick='window.opener.parentFunction()'>"+i+"</td>";
}

output += "</table></body></html>\n";
new_win.document.write(output);
}

* Je l'ai fait fonctionner. Merci les gars.

Était-ce utile?

La solution

Il y a un tas de problèmes avec votre code. J'ai mis en place une démo de travail ici.

Html

<button id="clickMe">Click me</button>

Javascrip

window.onload = function() {

    function parentFunction() {
        alert('testing');
    }

    window.parentFunction = parentFunction;

    var years = [1, 2, 3, 4, 5, 6];

    function print() {
        var new_win = window.open('', 'name', 'height=400,width=500');
        //var cols = this.getCols();
        var cols = 2;
        var output = "<html><head></head><body><table>";
        var cell_count = 1;
        for (i = 0; i < years.length; i++) {
            if (cell_count === 1) {
                output += "<tr>";
            }
            output += "<td onclick='window.opener.parentFunction();'>" + years[i] + "</td>";
            cell_count++;
            // end the row if we've generated the expected number of columns
            // or if we're at the end of the array
            if (cell_count > cols || i === years.length - 1) {
                output += "</tr>\n";
                cell_count = 1;
            }
        }
        output += "</table></body></html>";
        new_win.document.write(output);
    }

    document.getElementById('clickMe').onclick = print;
};
  • years n'a pas été défini
  • this.getCols() n'a pas été défini
  • parentFunction n'était (probablement) pas visible au window portée
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top