在一个函数中,我有一行代码,它打开一个窗口,同时从函数自己的参数(u.n)或函数内创建的变量中绘制其参数。
这在脚本中运行良好。
win2 = window.open(u, n, 'width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' + tools);
由于这在脚本中被调用了几次,但在 win3、win4 等中,为了减少代码,我想将每次都相同的参数放入一个变量中,然后每次都使用它。
myparameters = u + ',' + n + ',width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' + tools;
win3 = window.open(myparameters);
我尝试过这个,但运气不佳,可以吗?
谢谢。
最佳答案
是的,在某种程度上可以通过将其包装在函数调用中来实现。我通常做的是拥有一个实用函数,我可以在需要时调用它。
类似这样的事情:
popOpen: function (url, target, height, width) {
var newWindow, args = "";
args += "height=" + height + ",width=" + width;
args += "dependent=yes,scrollbars=yes,resizable=yes";
newWindow = open(url, target, args);
newWindow.focus();
return newWindow;
}
您可以通过将其设为对象来进一步减少参数,例如:
popOpen: function (params) {
var newWindow, args = "";
args += "height=" + params.height + ",width=" + params.width;
args += "dependent=yes,scrollbars=yes,resizable=yes";
newWindow = open(params.url, params.target, params.args);
newWindow.focus();
return newWindow;
}
你可以这样调用它:
var param = { url: '...', height: '...', width: '...' };
popOpen(param);
或者,
var param = new Object;
param.url = '...';
param.height = '...';
popOpen(param);
关于javascript - window.open 参数作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19660207/