Javascript-форум (https://javascript.ru/forum/)
-   Events/DOM/Window (https://javascript.ru/forum/events/)
-   -   TypeError: Illegal constructor Chrome (https://javascript.ru/forum/events/69279-typeerror-illegal-constructor-chrome.html)

vitorrio 10.06.2017 21:42

TypeError: Illegal constructor Chrome
 
Код работает в Opera, в Chrome не работает:
try
{
	function fire(){
		var press=new KeyboardEvent('keypress',{
			key:'a',
			bubbles:true,
			cancelable:true,
			isTrusted:true,
			keyCode:97,
			charCode:97,
			wich:97,
			view:window
		});
		document.body.dispatchEvent(press);
	};
	document.body.addEventListener("keypress",function(event){
			alert("Вы нажали "+event.key);
	},false);
	fire();
}
catch(err)
{
	alert(err);
}

рони 10.06.2017 22:45

vitorrio,
строка 11 which и может document.body ещё нет?

рони 10.06.2017 22:47

vitorrio,
<!DOCTYPE html>

<html>
<head>
  <title>Untitled</title>
  <meta charset="utf-8">
</head>

<body>

<script>

try
{
  function fire(){
    var press=new KeyboardEvent('keypress',{
      key:'а',
      bubbles:true,
      cancelable:true,
      isTrusted:true,
      keyCode:97,
      charCode:97,
      which:97,
      view:window
    });
    document.body.dispatchEvent(press);
  };
  document.body.addEventListener("keypress",function(event){
      alert("Вы нажали "+event.key);
  },false);
  fire();
}
catch(err)
{
  alert(err);
}


  </script>
</body>
</html>

vitorrio 10.06.2017 22:54

Та же самая ошибка, ошибка в конструкции "var press=new KeyboardEvent('keypress',{
"

vitorrio 10.06.2017 23:00

var keyEvent = document.createEvent('KeyboardEvent');
Object.defineProperty(keyEvent, 'keyCode', { 
                         get : function() {
                                 return this.keyCodeVal;
                         }
                        });
keyEvent.initKeyboardEvent('keypress', true, false, null, 0, false, 0, false, 77, 0);
keyEvent.keyCodeVal = 77;
document.body.dispatchEvent(keyEvent);

Вот так работает, правда не показывается символ


Часовой пояс GMT +3, время: 05:07.