Windows.event undefined -Javascript error in firefox

I use javascript to change some asp button settings on hover. It works in IE. But does not work in Firefox. Is there any other javascript code that will support almost all browsers? My code is as follows

<script type="text/javascript"> var previousColor; function Changecolor() { previousColor = window.event.srcElement.style.backgroundColor; window.event.srcElement.style.backgroundColor = "Blue"; window.event.srcElement.style.cursor = "hand"; } function RestoreColor() { window.event.srcElement.style.backgroundColor = previousColor; } </script> <asp:Button ID="btnSearch" runat="server" BackColor="#800000" Font-Bold="True" Font-Names="Arial" onmouseover="Changecolor();" onmouseout="RestoreColor();" ForeColor="White" Height="28px" OnClick="btnSearch_Click2" Text="Search Jobz" Width="117px" /> 
+4
source share
4 answers

Take a look at the Mozilla Developer Center docs for events . In Internet Explorer, a global event object is created when an event is triggered. In standards-compliant browsers, the event object is passed as the first argument to the function assigned to the dismissal event. If your event is defined in HTML, the event object is created under the name of the variable event and can be passed to the functions that you call.

Also note that the event.srcElement property is only IE, and most other browsers use event.target instead.

Given this, your function should look like this:

 <script> var previousColor; function Changecolor(evt) { var srcEl = evt.srcElement || evt.target; previousColor = srcEl.style.backgroundColor; srcEl.style.backgroundColor = "Blue"; srcEl.style.cursor = "pointer"; } function RestoreColor(evt) { var srcEl = evt.srcElement || evt.target; srcEl.style.backgroundColor = previousColor; } </script> <asp:Button ID="btnSearch" runat="server" BackColor="#800000" Font-Bold="True" Font-Names="Arial" onmouseover="Changecolor(event);" onmouseout="RestoreColor(event);" ForeColor="White" Height="28px" OnClick="btnSearch_Click2" Text="Search Jobz" Width="117px" /> 
+8
source

Why don't you use CSS (with its pseudo-frame :hover ) instead of JS?

But if you need to use JS for some reason, just pass the element reference as a function parameter:

 function Cangecolor(ref){ prevousColor = ref.style....; ... onmouseover="changecolor(this)" 

Now your code does not work because IE uses a different event model (good browsers use the W3C event model, but IE uses its own). You can read about W3C / IE event models at: How to Align IE and W3C Event Models ]

+1
source

You do not need to worry about the event. Also, the correct value for the CSS property of the cursor is a "pointer" and not a "specific" IE unless you need support for IE 5 or 5.5.

 <script type="text/javascript"> var previousColor; function changeColor(el) { var s = el.style; previousColor = s.backgroundColor; s.backgroundColor = "Blue"; s.cursor = "pointer"; } function restoreColor(el) { el.style.backgroundColor = previousColor; } </script> <asp:Button ID="btnSearch" runat="server" BackColor="#800000" Font-Bold="True" Font-Names="Arial" onmouseover="changeColor(this);" onmouseout="restoreColor(this);" ForeColor="White" Height="28px" OnClick="btnSearch_Click2" Text="Search Jobz" Width="117px" /> 
+1
source

I ran into the same problem (window.event in firefox) with the code below.

 <html> <body onkeyup="fClosePop();"> </body> <script language="javascript"> function fClosePop(){ var e= (window.event)?event.keyCode:evt.which; if( e.keyCode ==27) { try { alert(1); } catch(e) { } } } </script> </html> 

Then I tried to configure the code and found the solution below. I just passed an event from a function.

 <html> <body onkeyup="fClosePop(even);"> </body> <script language="javascript"> function fClosePop(e){ if( e.keyCode ==27) { try { alert(1) } catch(e) { } } } </script> </html> 

I hope this will be helpful to you.

+1
source

Source: https://habr.com/ru/post/1299090/


All Articles