Reputation: 20468
i do not know what is going on my firefox!
my aspx and javascript codes are like this :
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function a() {
alert('a');
//alert(event.which);
//alert(event.keyCode);
//alert(event.charCode);
}
function b() {
alert('b');
//alert(event.which);
//alert(event.keyCode);
//alert(event.charCode);
}
function c() {
alert('c');
//alert(event.which);
//alert(event.keyCode);
//alert(event.charCode);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" onkeyup="a()"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" onkeydown="b()"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" onkeypress="c()"></asp:TextBox>
</div>
</form>
</body>
</html>
when i type something in textbox 1,2,3 so i see just first alert (mean a,b,c).
what is the problem ?
thanks in future advance...
Upvotes: 0
Views: 4439
Reputation: 605
I also had a problem with trying to get something like this work for both FireFox and IE. What worked for me was to use the keyword event
as an input parameter for a function call to some javascript function.
What I wanted was to start up a new .js
file which will hold all my javascript so that all my forms can call functions in there. My implementation just allows for numbers, letters and the dash, CR and BS keys to be used from a text input box. Here is the javascript code I used which works for me:
//This function filters out character inputs from client-side
//that do not include a-z, A-Z, 0-9 and '-' (dash) and backspace
function charFilter(event) {
event = event || window.event || event.which || event;
//alert(event);
var unicode;
if (event.charCode) unicode = event.charCode;
else if (event.which) unicode = event.which;
else if (event.keyCode) unicode = event.keyCode;
else if (event) unicode = event;
// unicode must be: 0-9 OR A-Z
OR a-z OR - OR BS OR CR
if ((unicode > 47 && unicode < 58) || (unicode > 64 && unicode < 91) || (unicode > 96 && unicode < 123) || unicode == 45 || unicode == 8 || unicode == 13) {
return true;
}
else return false;
}
The code running the form Default.aspx
(Default.aspx.vb
) now contains the following Sub:
Protected Sub Page_load(ByvVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'some code
'Filter characters in text search field (client side)
Dim t As Type = Me.GetType()
Me.Page.ClientScript.RegisterClientScriptInclude("t", ResolveUrl("~/JScript.js"))
txtSearch.Attributes.Add("OnKeyPress", "return charFilter(event)")
'rest of code
End Sub
I think the other submission is much more clean code but this shows an example to get the javascript function in a separate path to work. Note that the path to the .js
file will be different for you, I just went with a default location for this.
Upvotes: 0
Reputation: 817128
It works in IE because there, event
is a global variable.
In Firefox, the event object is passed to the event handler, so you have to make the function accept a parameter:
function a(event) {
event = event || window.event // IE does not pass event to the function
alert('a');
alert(event.which);
alert(event.keyCode);
alert(event.charCode);
}
And in your HTML, you have to write:
<asp:TextBox ID="TextBox1" runat="server" onkeyup="a(event)"></asp:TextBox>
Btw. you could have easily spot this by using Firebug. It throws an error in the console.
Upvotes: 4