[英]window.open parameters as a variable
在一個函數中,我有一行代碼可以打開一個窗口,同時可以從函數自己的參數(un)或在函數中創建的變量中繪制其參數。
在腳本中可以正常工作。
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);
您嘗試的方式是不可能的。 您可能要這樣做:
var myparameters = 'width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' + tools;
win3 = window.open(u, n, myparameters);
小提琴: http : //jsfiddle.net/aBR7C/
您在函數調用中缺少一些其他參數:
var myparameters = 'width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' + tools;
win3 = window.open(u, n, myparameters);
^ ^ ^
//Here you are passing parameters
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.