Brijesh Maurya
Brijesh Maurya

Reputation: 125

Cross browser issue with script

I have written some JavaScript code as follows

window.onbeforeunload = function(myEvent) {
  myEvent.returnValue = "Bye Bye Fool!...";
}

var txt = ['Y', 'o', 'u', ' ', 'a', 'r', 'e', ' ', 'a', ' ', 'f', 'o', 'o', 'l', '.', ' '];
var value = document.getElementById('txtinput').value;
var lastNum = 0;
document.getElementById('txtinput').onkeypress = function() {
  myFunction()
}
document.getElementById('txtinput').onkeyup = function() {
  myFunction2(event)
}

function myFunction() {
  for (i = lastNum; i <= lastNum; i++) {
    value = value + txt[i];
  }
  if (i < txt.length) {
    lastNum = i;
  } else {
    lastNum = 0;
  }
  document.getElementById('txtinput').value = value;
}

function myFunction2(event) {
  var x = event.keyCode;
  var y = String.fromCharCode(x);
  value = String(value, y);
  document.getElementById('txtinput').value = value;
}
<textarea rows="10" cols="50" id='txtinput'></textarea>

I was nothing special but just for fun (actually I am a new programmer & trying to enhance my programming skills). It's working very well in Chrome, but in Firefox it has a issue. Here is the idea :

It will make a text area to type & when user will start typing it will only type "You are a fool." That it picks from array named 'txt'. So, once a key is pressed, it will print the letter and on releasing the key, it will remove the last printed letter (i.e. It will only show the letters from array & remove the pressed key letter).

Chrome is executing it very well but in Firefox, the onkeyup event is not working. It removes the pressed letter when next key is pressed.

Upvotes: 0

Views: 36

Answers (1)

Paul Roub
Paul Roub

Reputation: 36438

There's no local event variable here:

document.getElementById('txtinput').onkeyup = function(){myFunction2(event)}

It appears Chrome may be passing a global event object that's working for you.

If you want the keyup's event object to make it to your function, lose the wrapper:

document.getElementById('txtinput').onkeyup = myFunction2;

Upvotes: 1

Related Questions