简体   繁体   中英

window.open parameters as a variable

Within a function I have a line of code that opens a window whilst drawing its parameters either from the functions own parameters (un) or variables created within the function.

This works fine in the script.

win2 = window.open(u, n, 'width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' + tools);

As this is called several other times in the script but as win3, win4 etc. , to reduce code, I wanted to put the parameters, which are the same each time, into a variable and just use that each time.

myparameters =  u + ',' + n + ',width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' + tools;

win3 = window.open(myparameters);

I have tried playing around with this without much luck, can it be done?

Thanks.

Yes you can, to some extent by wrapping it in function call. What I typically do is to have a utility function which I can call upon whenever required.

Something like this:

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

You can further reduce the parameters by making it an object like:

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

And you can call it like this:

var param = { url: '...', height: '...', width: '...' };
popOpen(param);

Or,

var param = new Object;
param.url = '...';
param.height = '...';
popOpen(param);

The way you are trying is not possible. You might want to do this:

var myparameters =  'width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' + tools;
win3 = window.open(u, n, myparameters);

Fiddle: http://jsfiddle.net/aBR7C/

You are missing some additional parameters in your function call:

var myparameters =  'width=' + w + ', height=' + h + ', ' + 'left=' + wleft + ', top=' + wtop + ', ' + tools;

win3 = window.open(u, n, myparameters);
                   ^  ^   ^
                 //Here you are passing parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM