From: nick on
On Feb 2, 5:13 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
> Scott Sauyet wrote:
> ...
> > Several threads on FAQ questions had me randomly reading the FAQ, and
> > the entry on checking for an open child window [1] made me pause.  The
> > suggestion not to use pop-ups is fine, and there is nothing exactly
> > wrong with the code.  But it introduces a global variable as well as
> > the function.  This might well be what's wanted here, but often it's
> > just unwanted clutter, and I'm wondering what suggestions the experts
> > here have to avoid such clutter.
>
> Function rewriting?
>
> var openWin = function(aURL) {
>    var myWin;
>    return (openWin = function(aURL) {
>      if (!myWin || myWin.closed ) {
>         return window.open(aURL,'myWin');
>      } else {
>        myWin.location.href = aURL;
>        myWin.focus();
>        return myWin;
>      }
>    })(aURL);
>
> };

Why not just:

var openWin = function(aURL) {
var myWin;
return (function() {
if (!myWin || myWin.closed ) {
return window.open(aURL,'myWin');
} else {
myWin.location.href = aURL;
myWin.focus();
return myWin;
}
})();
};


....BTW Scott, most popup blockers seem to intercept this, but I guess
that's beside the point :)

-- Nick
From: Garrett Smith on
nick wrote:
> On Feb 2, 5:13 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
>> Scott Sauyet wrote:
>> ...
>>> Several threads on FAQ questions had me randomly reading the FAQ, and
>>> the entry on checking for an open child window [1] made me pause. The
>>> suggestion not to use pop-ups is fine, and there is nothing exactly
>>> wrong with the code. But it introduces a global variable as well as
>>> the function. This might well be what's wanted here, but often it's
>>> just unwanted clutter, and I'm wondering what suggestions the experts
>>> here have to avoid such clutter.
>> Function rewriting?
>>
>> var openWin = function(aURL) {
>> var myWin;
>> return (openWin = function(aURL) {
>> if (!myWin || myWin.closed ) {

That should be:
myWin = window.open(aURL,'myWin');
return myWin;

>> return window.open(aURL,'myWin');
>> } else {
>> myWin.location.href = aURL;
>> myWin.focus();
>> return myWin;
>> }
>> })(aURL);
>>
>> };
>
> Why not just:
>
> var openWin = function(aURL) {
> var myWin;
> return (function() {
> if (!myWin || myWin.closed ) {
> return window.open(aURL,'myWin');
> } else {
> myWin.location.href = aURL;
> myWin.focus();
> return myWin;
> }
> })();
> };
>

That will create a new function and execute that new function each time
openWin is called.

In contrast, the function re-writing replaces the identifier of the
containing scope to point to the function in the nested scope, but only
after executing it the first time.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: nick on
On Feb 2, 8:11 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
> nick wrote:
> ...
> > Why not just:
> > [code]
>
> That will create a new function and execute that new function each time
> openWin is called.
>
> In contrast, the function re-writing replaces the identifier of the
> containing scope to point to the function in the nested scope, but only
> after executing it the first time.

Ah, I see what you did there. So the "openWin = function ..." is where
the re-writing happens, missed that the first time around. That's a
pretty neat trick.

From: nick on
On Feb 2, 8:11 pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
>
> That will create a new function and execute that new function each time
> openWin is called.
>

Thinking about this a little more, would this also cause the inner
function to be created every time the outer is called (I'm not sure
how to test this)?

var openWin = function(aURL) {
var myWin;
return f();
function f() {
if (!myWin || myWin.closed ) {
window.open(aURL,'myWin');
return myWin;
} else {
myWin.location.href = aURL;
myWin.focus();
return myWin;
}
}
};

If f() does only get created once this might be a simpler syntax to
use.

-- Nick
From: Lasse Reichstein Nielsen on
nick <nick___(a)fastmail.fm> writes:

> On Feb 2, 8:11�pm, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
>>
>> That will create a new function and execute that new function each time
>> openWin is called.
>>
>
> Thinking about this a little more, would this also cause the inner
> function to be created every time the outer is called (I'm not sure
> how to test this)?

Yes it would, but the outer function will only be called once. After
that, the reference to the outer function would be overwritten.

Personally I would do it like:

var openWin = (function() {
var myWin;
return function(aURL) {
if (!myWin || myWin.closed ) {
myWin = window.open(aURL, 'myWin');
} else {
myWin.location.href = aURL;
myWin.focus();
}
return myWin;
};
})();


/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'