最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Windows.event is undefined -Javascript error in firefox - Stack Overflow

programmeradmin5浏览0评论

I'm using javascript to change some settings of asp button on mouseover. It is working in IE. But not working 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" />

I'm using javascript to change some settings of asp button on mouseover. It is working in IE. But not working 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" />
Share Improve this question edited Feb 10, 2012 at 10:09 Mormegil 8,0714 gold badges43 silver badges80 bronze badges asked Jan 22, 2010 at 9:16 Developer404Developer404 5,97217 gold badges69 silver badges104 bronze badges 2
  • 1 Why are you making it so difficult? Just use CSS. – ZippyV Commented Jan 22, 2010 at 9:24
  • Indeed, CSS is better for this sort of thing, although :hover won't work in IE6 without whatever:hover – Andy E Commented Jan 22, 2010 at 9:31
Add a ment  | 

4 Answers 4

Reset to default 8

Take a look at the Mozilla Developer Center docs on events. In Internet Explorer, the global event object is created when an event is fired. In standards pliant browsers, the event object is passed as the first argument of the function assigned to the firing event. If your event is defined in the HTML, the event object is created under the variable name event and can be passed to the functions you're calling.

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

Taking this into consideration, 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" />

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

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

function Cangecolor(ref){
  prevousColor = ref.style....;

...

onmouseover="changecolor(this)"

Right now your code doesn't work because IE uses different Event Model (good browsers uses W3C Event Model, but IE uses its own). You can read about W3C/IE event models at: How to equalize the IE and W3C event models]

You don't need to bother with the event object. Also, the correct value for the cursor CSS property is "pointer" rather than the IE-specific "hand", unless you need to support 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" />

I was facing the same (window.event in firefox) problem with below code.

<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 tweak the code and found the solution below. I have just passed the event from the 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.

发布评论

评论列表(0)

  1. 暂无评论