Results 1 to 6 of 6

Thread: javascript causing firefox to crash

  1. #1
    Join Date
    Jun 2008
    Posts
    107
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default javascript causing firefox to crash

    hello,

    The following code is causing firefox to crash:

    Code:
    if (!e)
    {
    	alert("!e");
    	e = window.event;
    }
    
    alert("before");
    var x = e.pageX;
    alert("after");
    By 'crash', I mean it just stops running. It doesn't give me an error message or shut down the browser, it just quits reading the script.

    I get the alert box "!e" and "before" but I don't get anything for "after". At the line var x = e.pageX; it seems to die. This seems to happen anytime I use e, for example e.clientX, e.screenX, e.type, etc.

    This doesn't occur in IE.

    Anyone know what the problem is?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,957
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    There is no window.event in Firefox. So, if e wasn't defined it still isn't. Usually e is passed automatically though when the event occurs. But depending upon how the function with that code in it is invoked or how the function itself is written, it might not. Probably not in this case considering the problem.

    If you want more help, please post a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2008
    Posts
    107
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    There is no window.event in Firefox. So, if e wasn't defined it still isn't. Usually e is passed automatically though when the event occurs. But depending upon how the function with that code in it is invoked or how the function itself is written, it might not. Probably not in this case considering the problem.
    Correct.

    the function is setup as a listener for mouseup events dynamically. There's a button on the page, and when that button is click, some function is called which does this:

    someFunction()
    {
    ...
    document.onmouseup = new Function(mouseupListener());
    ...
    }

    where mouseupListener() is the function that my code resides in.

    It does no good to pass in the event that invoked the call to someFunction because that's a totally different event than the mouseup event I'm interested in.

    Quote Originally Posted by jscheuer1 View Post
    If you want more help, please post a link to the page on your site that contains the problematic code so we can check it out.
    Even if I knew where the web site was (I'm working on it in Visual Studios on my local machine, and I'm not sure where my boss publishes it to once I'm done), you'd need credentials to get in and I have no freebies to hand out.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,957
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    This is incorrect syntax and will run or attempt to run mouseupListener as soon as it is parsed without assigning it to any mouse behavior:

    Code:
    document.onmouseup = new Function(mouseupListener());
    It should be (maybe it is, maybe you just left out the quote marks):

    Code:
    document.onmouseup = new Function("mouseupListener()");
    I would at least try changing it to:

    Code:
    document.onmouseup = mouseupListener;


    Or perhaps you meant:

    Code:
    document.onmouseup = function(){mouseupListener()};
    In which case the solution is the same and you can also do:

    Code:
    document.onmouseup = function(e){mouseupListener(e)};


    But no matter how you do it, for it to have any hope of working in Firefox, the mouseupListener function should look something like so:

    Code:
    function mouseupListener(e){
    if (!e)
    {
    	alert("!e");
    	e = window.event;
    }
    
    alert("before");
    var x = e.pageX;
    alert("after");
    }
    or so:

    Code:
    var mouseupListener = function(e){
    if (!e)
    {
    	alert("!e");
    	e = window.event;
    }
    
    alert("before");
    var x = e.pageX;
    alert("after");
    }
    If you want more help, perhaps you could replicate the problem on a simpler scale and paste in the code here.

    BTW - Prior to IE 9, there is no e.pageX in Internet Explorer. And even in IE 9, if you are relying upon window.event - it still has no e.pageX.
    Last edited by jscheuer1; 02-18-2012 at 12:02 AM. Reason: clarity
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2008
    Posts
    107
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Thanks jscheuer1,

    Sorry I haven't replied in a while - been busy.

    I've implemented something similar to what you suggested. In essence, e had to be in the argument list of the function, and then Firefox wouldn't enter the if(!e) condition (but IE would, but IE has no problem with this).

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,957
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    I should have been a little clearer about e.pageX in IE. Prior to IE 9, it won't cause an error. But it will return the value as undefined. Even with IE 9, if IE 9 is somehow forced to use window.event for e in this situation, it will do the same - x will equal undefined.

    There are I think only two ways IE 9 could be forced to use window.event in this scenario:

    1. If the page is in quirks mode
    2. If no standard event model event is passed to the function as e


    Like if your page is only this:

    Code:
    <script type="text/javascript">
    var mouseupListener = function(e){
    if (!e)
    {
    	alert("!e");
    	e = window.event;
    }
    
    alert("before");
    var x = e.pageX;
    alert(x);
    }
    document.onmouseup = mouseupListener;
    </script>
    All IE will alert undefined for x. Firefox and others will alert a number - the x coordinate of the event.

    If you add a standards invoking DOCTYPE:

    Code:
    <!DOCTYPE html>
    <script type="text/javascript">
    var mouseupListener = function(e){
    if (!e)
    {
    	alert("!e");
    	e = window.event;
    }
    
    alert("before");
    var x = e.pageX;
    alert(x);
    }
    document.onmouseup = mouseupListener;
    </script>
    IE 9 will now alert X as a number like the others do. But IE 8 and less will still be undefined.

    To work in all IE as well as other browsers, you need (with or without the DOCTYPE):

    Code:
    <script type="text/javascript">
    var mouseupListener = function(e){
    if (!e)
    {
    	alert("!e");
    	e = window.event;
    }
    
    alert("before");
    var x = typeof e.pageX === 'number'? e.pageX : e.clientX;
    alert(x);
    }
    document.onmouseup = mouseupListener;
    </script>
    Last edited by jscheuer1; 02-22-2012 at 03:07 PM. Reason: add examples
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •