[thelist] not sure if this went through -- if so sorry for repost

.jeff jeff at members.evolt.org
Fri Jun 15 19:16:25 CDT 2001


fortune & rudy,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: rudy
:
: > <a
: >
href="javascript:popUp('mypage.htm','PopUpWindow','width=520,height=640')"
: > >link</a>
:
: try this instead
:
: <a href="mypage.htm"  target="_blank"
:    onClick="popUp('mypage.htm', 'PopUpWindow', 'width=520,height=640');
:             return false;"
: >link</a>
:
: when you use the javascript protocol as the href,
: i *think* (willing to be corrected -- jeff?) what
: happens is that even if the popup has self.focus,
: the parent window gets focus after executing the
: link
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

that's been my experience.  however, there have been a number of changes
made to the way window focus works in the operating system as a whole.
apparently it is microsoft's view that there are applications that are built
to take too much focus priority away from other applications (things like
instant message apps, for example), but i digress.

i suspect the problem being encountered is a symptom of this.

i would like to make it very clear that there is no *guaranteed* way of
reliably bringing focus to a child window.  you can achieve about 95%
reliability, but it's hit and miss above that.

some slight modifications to your popUp() function might be in order too.
first of all, it's good practice to test for support of a method before
using it.  since the focus() method is supported by the window object since
js1.1, that means that js1.0 browsers (few as they may be is not an excuse)
will not recognize the method and throw an error.

second, when opening the window you should assign it to a variable so you
can do things with it later.  the way you've got it now, you're opening a
new window, then executing the next line which gives focus back to the
calling window -- not the desired effect and probably contributing to your
reliability issues.

i would change the function to something like this:

var popUpWin = null;

function popUp(theURL,winName,features)
{
  popUpWin = window.open(theURL,winName,features);
  if(popUpWin.focus) popUpWin.focus();
}

now, you've got a choice to make.  you can either make an effort to include
an onLoad event handler in every page that will open in the popup with a bit
of script to focus the popup or you can use the focus() method mentioned
above.  if you use both in ie you will sporadically generate errors.  my
preference, for maintenance reasons would be to not use the onLoad event
handler within every document that will be opened in the popup in favor of
using the method in your popUp() function.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: the suggested format is better because the link
: will work even if javascript is turned off
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

or heck, you could go a step further to improve maintainability:

<a href="mypage.htm"
   target="PopUpWindow"
   onClick="popUp(this.href, this.target, 'width=520,height=640');
            return false;"
>link</a>

in this use, the "this" keyword refers to the tag that contains the event
handler.  using it like this you can access the attributes of the tag as
properties of the "this" keyword.

this.href is the same as manually typing 'mypage.htm' as the first argument
to pass to the popUp() function.  this.target is the same as manually typing
'PopUpWindow' as the second argument to pass to the popUp() function.

good luck,

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/






More information about the thelist mailing list