2

Within a function I have a line of code that opens a window whilst drawing its parameters either from the functions own parameters (u.n) 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.

3
  • 1
    I don't think you can. Commented Oct 29, 2013 at 13:57
  • @putvande - just to reduce the amount of code Commented Oct 29, 2013 at 13:58
  • you're trying to pass a single string whereas all those are expected as individual parameters. each parameter has a different meaning. Refer developer.mozilla.org/en-US/docs/Web/API/Window.open Commented Oct 29, 2013 at 14:03

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

And how are you going to execute this function with only 1 variable?
@putvande while you were downvoting, I was editing the answer to include an object variable. This makes it one variable to pass. Is that good enough?
Not sure.. is up to OP to decide I guess. But it still has the same problem. (I have removed the downvote though)
The OP doesn't really want to call a function with "one variable". He wants one variable that he can call window.open with that changes "magically" each time he uses that variable. What the OP really wants is a function.
Just wrapping the call. It will still be a function call for the op, but with reduced code every time he makes a call. And yes, thanks a lot @putvande for considering.
1

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/

5 Comments

@martin what problem you are getting?
He wants the variable to be "dynamic", that is, each time he uses it the w and h gets a new value. He's got a big misunderstanding of how variables work. what he really wants is a function.
Edited the answer and added an example running at jsfiddle. It seems to be working fine.
Yes, I found a typo in my code, this does seem to work. I will just double check it works with all browsers, then mark this as my answer. Thanks.
Works okay in FF, but with Chrome I am getting windows that aren't sized correctly, so it probably isn't wise to do it this way.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.