Connecting Tech Pros Worldwide Help | Site Map

focus() not working in IE when created by appendChild

Newbie
 
Join Date: Aug 2007
Posts: 6
#1: Aug 30 '07
I have a main window and a menu window.
The menu window is opened from the main window by clicking on "Open Menu".
A google window can also be opened from the main window by clicking "Open Google". If "google_win" is clicked on in the menu window the google window is focused on.

Now if "Add Google to Menu" is clicked in the main window, another line is written to the menu window also saying "google_win".
This second "google_win" is added by using appendChild and has the same .....focus() code behind it as the first "google_win". But if this second one is clicked the google window is NOT focused on when IE is used. I've tried IE6 and 7 on XP and IE5.2 on OSX. This does work in Firefox and Safari.

This is the main window code:
[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Untitled</title>
</head>
<script type="text/javascript">
<!--
function opengoogle(){
address="http://www.google.com/"
google_win=window.open(address,'google_win')
}
function addtomenu(win_name){
sectRef = menuwin.document.getElementById('menu_list');
newLine = menuwin.document.createElement("br");
sectRef.appendChild(newLine);
newSpan = menuwin.document.createElement("span");
openscript = "window.opener." + win_name +".focus();"
newSpan.setAttribute('onClick',openscript);
newText = menuwin.document.createTextNode(win_name);
newSpan.appendChild(newText);
sectRef.appendChild(newSpan);
}
function openmenu(){
menuwin=window.open('menutest1.html','Menu','width =200,height=600,resizable=yes,scrollbars=auto,tool bar=yes,location=no,directories=no,status=yes,menu bar=yes,copyhistory=no,left=0,top=100,screenX=0,sc reenY=100');
}
//-->
</script>
<body>
<span onClick="openmenu()">Open Menu</span><br>
<span onClick="opengoogle()">Open Google</span><br>
<span onClick="addtomenu('google_win')">Add Google to Menu</span>
</body>
</html>[/HTML]

This code is for the menu window and belongs in a file called "menutest1.html"
[HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Menu</title>
</head>
<body>
<div id='menu_list'>
<span onClick='window.opener.google_win.focus();'>google _win</span>
</div>
</body>
</html>[/HTML]

These pages are at: http://insitefo.com/domtest1.html

Any ideas how to get the added "google_win" line to work properly or why it's not working?

Thanks
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#2: Aug 30 '07

re: focus() not working in IE when created by appendChild


Fist of all you commented the Script Codes.
Anyway, the picture is not clear to me.
Actually what I understood that is if you click on Add Menu then the menu will be added there and the Menu_List window will be focused.
Right?
You want this?

Kind regards,
Dmjpro.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: Aug 30 '07

re: focus() not working in IE when created by appendChild


On line 20,
Expand|Select|Wrap|Line Numbers
  1. newSpan.setAttribute("onclick",...);
will not work in IE. Try:
Expand|Select|Wrap|Line Numbers
  1. newSpan.onclick=...;
instead.
Newbie
 
Join Date: Aug 2007
Posts: 6
#4: Sep 11 '07

re: focus() not working in IE when created by appendChild


Quote:

Originally Posted by acoder

On line 20,

Expand|Select|Wrap|Line Numbers
  1. newSpan.setAttribute("onclick",...);
will not work in IE. Try:
Expand|Select|Wrap|Line Numbers
  1. newSpan.onclick=...;
instead.

I got that to work after a bit of work. I changed that setAttribute line to:
Expand|Select|Wrap|Line Numbers
  1. newSpan.onclick= function() {eval(win_name+".focus()");}
Thanks for the help.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Sep 11 '07

re: focus() not working in IE when created by appendChild


Glad to hear you got it working.

You don't need to use eval. You could use [] notation, e.g. replace
Expand|Select|Wrap|Line Numbers
  1. eval("window.opener." + win_name + ".focus();");
with
Expand|Select|Wrap|Line Numbers
  1. window.opener[win_name].focus();
Reply