SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can't call window.open() from a keyPress event?

    I've got a script which is called by a keypress event, something like this:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function respondToKey(e) {
      keyPressed = String.fromCharCode(e.which);
      if(keyPressed == 'g'){
        alert('you pressed the g key');
        window.open('http://google.com');
      }
    }
    
    </script>
    	<title>Untitled</title>
    </head>
    <body onkeypress="respondToKey(event)">
    hit the g key to open a google window
    </body>
    </html>
    when you hit the right key, the alert appears, but the window.open() never happens. There's not even a message in the Error Console, just nothing.

    Is this a security feature? Any way I can get around it?

    Using Firefox, 2.0.0.9 by the way.

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out your preferences, Firefox might be configured to only open pop-up windows when you click a link or button. (which is the case in my own copy of FF)

  3. #3
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, that's it. Thank you! It's now working.

    I guess, although it's a response to a user action because I've said "hit key to do X", typing the letter "g" is not the kind of user action you want to launch external windows.

  4. #4
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope
    I think you need to find another way of launching that window, since most browsers do exactly the same as Firefox in this situation, by default even.

  5. #5
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something is fundamentally wrong with above code. It wouldn't work in IE.

    First off, In IE event object isn't passed on to the event handler as its first argument. Hence 'e' would be undefined in IE. In order to get around this you'll have to define the event object within the event handler as this
    var e = e || window.event (in IE event object is stored as the property of the window object)

    Next, keypress is an annoyingly tricky event. For one, in most cases it doesn't contain information like what key was pressed. Secondly, it is known to have some tricky differences in IE and FF.

    Third, 'which' property of event object is not only undefined in IE it is also deprecated in FireFox (or Netscape for that matter). So its not going to work in IE. you are better off using something like 'keyCode'.

    In effect, If I were you I would rewrite the above code as this:

    Code HTML:
    <html>
    <head>
    <script type="text/javascript">
    function respondToKey(e) {
      var e = e || window.event;
      var keyPressed = String.fromCharCode(e.keyCode);
      alert(keyPressed);
      if(keyPressed == 'G'){
        alert('you pressed the g key');
        window.open('http://google.com');
      }
    }
     
    </script>
        <title>Untitled</title>
    </head>
    <body onkeyup="respondToKey(event)">
    hit the g key to open a google window
    </body>
    </html>

    And as Ize specified in the previous posts, most modern browsers would block popup that open a page in an external domain (though not the one that is in the same domain in which the popup script is defined)

  6. #6
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that, although I don't quite agree that the code is fundamentally broken because it doesn't work in IE! It will be used exclusively on Firefox.

    I didn't know about the deprecation, and I definitely will fix that part of it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •