0

Is there a way we can include a keypress in the URL? We have a usecase where a webpage shows an additional information pane when a user presses 'i' on her keyboard. We want to show this additional information pane by default, without the user having to press the key 'i'.

Simply visiting this URL should automatically trigger a press on the 'i' key and show the information pane by default. Is there a way to construct such a URL?

EDIT: To provide more clarity, note that we do not control the webpage in question here. We're simply providing a URL such as www.google.com to our users, visiting it and pressing 'i' unhides the information pane class. We want to avoid having our users press 'i', by providing them a URL that automatically triggers a keypress event, something along the lines www.google.com?&keypress=i.

dmkathayat
  • 393
  • 1
  • 4
  • 11
  • Possible duplicate of [Is it possible to simulate key press events programmatically?](https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically) – Karan Shishoo Jan 19 '18 at 05:42

1 Answers1

0

why don't you call something like this onLoad:

var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' 
? "initKeyboardEvent" : "initKeyEvent";


keyboardEvent[initMethod](
               "keydown", // event type : keydown, keyup, keypress
                true, // bubbles
                true, // cancelable
                window, // viewArg: should be window
                false, // ctrlKeyArg
                false, // altKeyArg
                false, // shiftKeyArg
                false, // metaKeyArg
                40, // keyCodeArg : unsigned long the virtual key code, 
else 0
                0 // charCodeArgs : unsigned long the Unicode character 

//    associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);

Wrap all of this in an onLoad call and you will be able to make a keypress on page load. Make sure to replace 40 with the keyCode of 'i'.

The snippet has been taken from here

Kumar Shubham
  • 484
  • 6
  • 17
  • Edited my question further. We do not control any code on the webpage. The most we can do is to append to the URL that can automatically trigger a keypress event on page load. – dmkathayat Jan 19 '18 at 05:50
  • That's borderline possible I guess, however, it may be possible that there would be some other way. I looked https://stackoverflow.com/questions/4163879/call-javascript-function-from-url-address-bar and the above approach can't work according to this link. – Kumar Shubham Jan 19 '18 at 06:08
  • Better go for something like a chrome extension , which can control the page behaviour. – Kumar Shubham Jan 19 '18 at 06:09