Javascriptのウィンドウを使用する適切な方法は何ですか。オープナー?

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

質問

ポップアップウィンドウを作成しましたが、その中で親ウィンドウで作成した関数を使用したいと思います。私はwindowを使ってみました。オープナーparentFunction()しかし、役に立つことを知っています。他の誰かがこの問題を経験しましたか?私のコードは次のようになります。

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);
}

*それは働いて得ました。ありがとうみんな。

役に立ちましたか?

解決

あなたのコードにはたくさんの問題があります。私は作業デモをまとめました ここに.

HTML

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

ジャバスクリプト

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 定義されていませんでした
  • this.getCols() 定義されていませんでした
  • parentFunction (おそらく)で表示されていませんでした window スコープ
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top