这是我的职责;

function ButtonClickAction3 (zEvent) {

    var var4 = prompt ("text");
    var var1 = prompt ("text")
    var var2 = prompt ("text");
    var var3 = prompt ("text");

    win1 = window.open("MYURL1,"", "win1", "width=100,height=100,resizable");
    setTimeout("win1.close()",500);
    win2 = window.open("MYURL2,"", "win2", "width=100,height=100,resizable");
    setTimeout("win2.close()",500);
    win3 = window.open("MYURL3,"", "win3", "width=100,height=100,resizable");
    setTimeout("win3.close()",500);

}


当我将此代码与油脂猴子一起使用时;

win1 = window.open("MYURL,"", "win1", "width=100,height=100,resizable");

在window1打开“ win1.close()”命令后,该命令不起作用。提示“ win1未定义”。

但是,当我从控制台打开带有此代码的窗口时;

win1 = window.open("MYURL1,"", "win1", "width=100,height=100,resizable");


win1定义好了,我可以用win1.close()关闭它

我如何将其与油猴一起使用?

最佳答案

首先,您需要使用win1关键字赋予var变量作用域。其次,优良作法是将函数作为函数而不是字符串传递给eval()

尝试这个:

var var4 = prompt ("text");
var var1 = prompt ("text");
var var2 = prompt ("text");
var var3 = prompt ("text");

var win1 = window.open("MYURL1", "win1", "width=100,height=100,resizable");
setTimeout(function(){win1.close();},500);

var win2 = window.open("MYURL2", "win2", "width=100,height=100,resizable");
setTimeout(function(){win2.close();},500);

var win3 = window.open("MYURL3", "win3", "width=100,height=100,resizable");
setTimeout(function(){win3.close();},500);

关于javascript - Javascript错误:未定义win1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6640051/

10-11 22:19