Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

window.open popup works with IE and Epiphany, but not with Mozilla

I can't get window.open to work right with Mozilla even though it works fine with IE (Windows XP) and Epiphany (X Windows). By "working right" I mean that the URL I call with window.open will popup on the working browsers. On Mozilla the new URL appears in the same browser, removing the page that executed window.open.

The following code replicates the problem I described. The purpose of the code is to open a popup when I enter the <Enter> key inside the text field:

<html>
<head>
<script language=javascript>
function handleKeyPress(evnt)
{
  var keyCode = evnt.keyCode ? evnt.keyCode : evnt.which ? evnt.which : evnt.charCode;
  if(keyCode == 13)
  {
    window.open("http://www.google.com","test");
    return false;
  }
  return true;
}
</script>
</head>

<body>
<form action=index.php method=post>
<input type=text name=myval onkeypress="return handleKeyPress(event);">
<input type=submit>
</form>
<script>
window.open("http://www.google.com","test");
</script>
</body>
</html>
0
jarnold21
Asked:
jarnold21
  • 2
1 Solution
 
thirdCommented:
i'm not sure what you're trying to accomplish but try

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
document.onkeyup=handleKeyPress;
function handleKeyPress(e)
{
     var keyCode=window.event?event.keyCode:e.which;
     var srcElement=window.event?event.srcElement:e.target;
     
     if(keyCode==13 && srcElement.form)
     {
         window.open("http://www.google.com","test");
             return false;
     }
       return true;
}
</script>
</head>
<body>
<form action=index.php method=post onsubmit="return false;">
<input type=text name=myval>
<input type=submit>
</form>
</body>
</html>
0
 
thirdCommented:
but this simple code does the same thing,

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
function newWindow(){
  window.open("http://www.google.com","test");
  return false;
}
</script>
</head>
<body>
<form action=index.php method=post onsubmit="return newWindow();">
<input type=text name=myval>
<input type=submit>
</form>
</body>
</html>
0
 
jarnold21Author Commented:
I appreciate the last two answers, but I the advice because I need to react to the <Enter> key from the text field. The interface that I'm developing has many text fields and each one directs the focus another field depending on the data input before the <Enter> key is pressed.

I guess the bottom line of my question is: Is there a problem, or something that I need to watch for, when executing window.open inside a JavaScript function that is executed as a result of onKeyPress? This question specifically refers to the Mozilla browser. I have absolutely no problem with IE, but I need to get my interface to work with Mozilla too.
0
 
ljo8877Commented:
Two issues in the code. One is that "which" is obsolete: you should use keyCode for both IE and Mozilla. Two, charCode is 0 for control (or non-character keys) and keyCode contains the 13. It is always 0 for keyDown and keyUp, Check the result of these events at this page:

http://lawrence.ecorp.net/inet/departments/samples/html/keycodes.html

I haven't encountered any difference in window.open between IE and Mozilla accept for the coordinate names. Try changing the window name from "test" to something else. Window.open will load the page in the named window if there is one open with that name.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now