open webpage and simulate key press events

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Joshua2010
    New Member
    • May 2010
    • 3

    open webpage and simulate key press events

    I have been trying to merge two command scripts, and have gotten nothing. I am trying to use javascript commands to open a new window on Firefox, and on that opened window simulate a Tab key press.

    This is the full script that I have come up with so far to open two windows, with the websites being yahoo.com and google.com. ***To fully help me please copy this script, paste to Notepad, save as a .html file and run it on Firefox. You will notice a "Open Windows" button will appear. When you click on it the windows will open:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function open_win() {
    window.open("http://yahoo.com", "newwindow1")
    window.open("http://google.com", "newwindow2")
    }
    </script>
    </head>
    <body>
    <form>
    <input type=button value="Open Windows" onclick="open_win()">
    </form>
    </body>
    
    </html>


    Now this is what I have come up to simulate a Tab key event when you press the ENTER key:


    Code:
    <script language="javascript>
    function fakeTab() {
      if (window.event.keyCode == 13 && window.event.srcElement.tagName == "INPUT")
         window.event.keyCode = 9;
    }
    documet.onkeydown = fakeTab;
    </script>

    What I want to do is merge these two scripts, so that when I press that "Open Windows" button, the new window opens and the Tab key press is immediately simulated to move to the next field on the webpage.

    I understand that JavaScript is event driven, meaning some event has to occur before you can make things happen. The user must push a key or click somewhere on the page before you can relaly do anything with an event. I want to have the clicking of the "Open Windows" button, that first triggering event which opens a window and simulates the tab key press on that window.
    Last edited by gits; May 9 '10, 08:50 AM. Reason: added code tags
  • Joshua2010
    New Member
    • May 2010
    • 3

    #2
    How to open new window and simulate key press events

    I have been trying to merge two command scripts, and have gotten nothing. I am trying to use javascript commands to open a new window on Firefox, and on that opened window simulate a Tab key press.

    This is the full script that I have come up with so far to open two windows, with the websites being yahoo.com and google.com. ***To fully help me please copy this script, paste to Notepad, save as a .html file and run it on Firefox. You will notice a "Open Windows" button will appear. When you click on it the windows will open:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function open_win() {
    window.open("http://yahoo.com", "newwindow1")
    window.open("http://google.com", "newwindow2")
    }
    </script>
    </head>
    <body>
    <form>
    <input type=button value="Open Windows" onclick="open_win()">
    </form>
    </body>
    
    </html>


    Now this is what I have come up to simulate a Tab key event when you press the ENTER key:


    Code:
    <script language="javascript>
    function fakeTab() {
    if (window.event.keyCode == 13 && window.event.srcElement.tagName == "INPUT")
    window.event.keyCode = 9;
    }
    documet.onkeydown = fakeTab;
    </script>

    What I want to do is merge these two scripts, so that when I press that "Open Windows" button, the new window opens and the Tab key press is immediately simulated to move to the next field on the webpage.

    I understand that JavaScript is event driven, meaning some event has to occur before you can make things happen. The user must push a key or click somewhere on the page before you can relaly do anything with an event. I want to have the clicking of the "Open Windows" button, that first triggering event which opens a window and simulates the tab key press on that window.
    Last edited by Markus; May 9 '10, 01:26 PM. Reason: Added [code] tags

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      for your specific task you don't need to fire a keypress event ... which wouldn't even work ... you would just need to use the focus() method ... here is an example ...

      kind regards

      Comment

      • Joshua2010
        New Member
        • May 2010
        • 3

        #4
        Thanks. Tell me if I am doing this write. Let's say I want to focus on the text area of two separate websites (e.g. google.com, yahoo.com). I would open the windows by using this first:

        Code:
        <html>
        <head>
        <script type="text/javascript">
        function open_win() {
        window.open("http://google.com", "newwindow1")
        window.open("http://yahoo.com", "newwindow2")
        }
        </script>
        </head>
        <body>
        <form>
        <input type=button value="Open Windows" onclick="open_win()">
        </form>
        </body>
        
        </html>
        and then to focus on the text area for both of those window I would us this:


        Code:
        <html>
        <body>
        <script language="JavaScript">
            function function1() {
                document.all.myElement.focus();
            }
            function function2() {
                alert("onFocus event has been fired");
            }
        </script>
        <input id="myElement" type="text" value="" onfocus="function2();">
        <input type="button" 
               value="Give focus to the text input element and fire the event" 
               onclick="function1();">
        </body>
        </html>
        But, how do I make sure it knows that I want it focus on the text areas of the two windows I opened?
        Last edited by gits; May 11 '10, 07:53 AM. Reason: added code tags

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          ??? huh? ... you could only focus one element at a time, it should set the focus onload of the window ... ahh ... and when you include a page from another domain to that window ... as you do ... you cannot script it ... it's for security reasons ... and your script will throw an error since that would be considered a XSS (cross site scripting) attack ...

          kind regards

          Comment

          Working...