Click to See Complete Forum and Search --> : Shift focus to a new window
tobywallis
03-13-2003, 12:20 PM
Hi. I have a page which spawns another (popup) which shows an autogenerated password. The new window is called Password.
I want to put a link on the parent page which will shift focus to the Password window when clicked, so the punter doesn't think the password window has closed if it just loses focus to the main page. (IYSWIM)
I've tried <a href=javascript:Password.focus()> and <a href=javascript:window.Password.focus()> and almost every other combination I can think of but they all show an error.
Any help, anybody?
Thanks a bunch
===Toby===
AdamBrill
03-13-2003, 12:29 PM
This will make it blink on the task bar:
<a href="javascript:win.focus()">focus</a>
Notice the quotes... But, I think this would be closer to what you really want. This should go in the head:
win = null;
function focusme(){
if(win){
win.close();
}
win = window.open();
}
And this is the link:
<a href="wherever.htm" target="blank" onclick="focusme(); return false;">focusme</a>
I hope that helps... ;)
tobywallis
03-13-2003, 04:43 PM
Thanks, Adam, but I think not quite...
The window that changes the focus is the *parent* window, not the child. So it must identify the window of the child (by name, presumably) and the routine wouldn't be "focus me" but "focus Password" ... or something?
Thanks
===Toby===
<html><head>
<script>
function focusPwd(){
var pwd = window.open("file.html","pwd");
if(window.blur()){ pwd.focus(); }
}
</script>
</head><body>
<a href="javascript:focusPwd()">Open</a>
</body></html>
That should work.
tobywallis
03-13-2003, 05:34 PM
Thanks, Jona. But I don't want to *open* the window on the link; it already exists. All I want to do is change focus to it.
Or am I missing something?
===Toby===
Ok. But the window has to have a name. So how is the window opened? If you're using target="_blank" in the <A> tag, then change it to, target="windowName" And then use this:
<html><head>
<script>
function focusPwd(){ if(window.blur()){ pwd.focus(); } }
</script>
</head><body onload="focusPwd()">
</body></html>
That should do it.
tobywallis
03-14-2003, 04:31 AM
Dear Jona,
The new window is opened onLoad using the following:
<body onload="window.open('http://window.URL','Password','top=400,left=10,width=450,height=250,resizable')"> (so I *think* the window is called Password, but is the window title the same as Target=? If not maybe I should add a Target= into the above)
Then later I try to shift focus to it using
<a href="javascript:Password.focus()">Click here to bring password window to the front</a>
Does that hlp?
Thanks!
===Toby===
tobywallis
03-14-2003, 05:14 AM
Hi Jona,
I did it - changed the way the popup window is opened & wrote 2 functions:
<head>
<script>
var fs = null;
function PasswordScreen(url)
{
fs = window.open(url,'Password','top=400,left=10,width=450,height=250,resizable');
fs.focus();
return true;
}
function focusPasswordScreen()
{
if(fs != null && fs.open && fs.focus)
fs.focus();
}
</script>
</HEAD>
<body onload="PasswordScreen('http://window.URL')">
<a href="javascript:this.focusPasswordScreen()"><i>Click here to show Password Window</i></a></p>
</body>
Thanks for all your help
===Toby===
Yah, it's no problem. About the target= though. Target=windowName but the only problem with that is it have to be either a global variable or only inside the function. Otherwise windowName = undefined.
tobywallis
03-15-2003, 04:49 AM
Thanks I'll remember that...
Over and out