0

Could anyone tell me why this function won't execute in Firefox. The rest of the Javascript functions throughout the file work successfully except for this one (it flat out won't execute, no errors in the console). I suspected it was the pageX property, however that doesn't seem to be the case.

var prevX = 0;
var i = 0;
var drgleft = 0;
var drgright = 0;


function sequence_1(event){
    if(prevX == 0){
        prevX = event.pageX;
        return false;
    }  
    //drag left
    if(prevX > event.pageX){
        console.log('dragged left');
        drgleft++;
        if(drgleft == 2){
            drgleft = 0;
            i--;
            if(i < 0){
                i = 30; //for optimization reasons, input the cache.length value manually (this avoids unnecessary errors in the console and laggy framerate as a result).
            } 
            document.getElementById("TheBigOne").src = cache[i].src; //use console.log(i); as a method of verifying that the code is executing correctly
        }
    }
    else if(prevX < event.pageX){
        console.log('dragged right');
        drgright++;
        if(drgright == 2){
            drgright = 0;
            i++;
            if(i > 30){ //for optimization reasons, input the cache.length value manually (this avoids unnecessary errors in the console and laggy framerate as a result).
                i=0;
            }
            document.getElementById("TheBigOne").src = cache[i].src;
        }
    }
    else{
    }
    prevX = event.pageX
}

EDIT:

In my first post I didn't include the relevent HTML code that calls the function, for that I apologise and will implement it below.

 <!DOCTYPE html>
    <html>
       <body>
          <div class="The_main_event" draggable = "true" ondrag="sequence_1(event)" id = "GD">

              <img src="file:///C:/Users/Harry/Desktop/Website/Web_aeroplane/Web%20Test.0031.png" id="TheBigOne">

           </div>
    </body>
  </html>
  • 1
    What calls this function? What type of event is `event`? It'd be helpful if you could condense your problem into a single, self-contained HTML page that works on all the browsers but Firefox. – Blender Jan 30 '17 at 05:07
  • 1
    it's interesting to note that firefox is the only browser without the internet explorer kludge global `event` object - not that that should be an issue in this code, but, you never know – Jaromanda X Jan 30 '17 at 05:13
  • It's apparently an event handler. Please post the code where you attach that event handler. The problem is probably there. – ibrahim mahrir Jan 30 '17 at 05:14
  • @JaromandaX the `event` variable here isn't the global `event` you're talking about. It's the argument of the function so it's defined. – ibrahim mahrir Jan 30 '17 at 05:15
  • I know, just an interesting observation, and depending on how the function is invoked, may end up being more than just an observation - without seeing how this function is tied to some event, it's just speculative on my part – Jaromanda X Jan 30 '17 at 05:17

3 Answers3

1

Open Firefox's debugger (Ctrl + Shift + S on Windows) set a breakpoint at the first line of the function body. The contents of event will be inspectable. You can step through the function (F10 in Windows) to see what's happening. If the event is not directly from mouse/touch, it may not have a pageX property, therefore have no effects.

Walf
  • 8,535
  • 2
  • 44
  • 59
0

Is it a UIEvent, or a MouseEvent?

https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/pageX

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/pageX

UIEvents aren't standard and the documentation linked above has Firefox as unknown compatibility.

I'm guessing when you say it won't run, you mean, it's not working how you expect it to. It looks like in your case it is either returning false, or is doing nothing except trying to update prevX. If event.pageX is undefined and therefore prevX is undefined, it would cause your function to have no effect and appear to not run.

EDIT: After seeing your update, it looks like it's because the event you're listening to doesn't have pageX. See here for documentation: https://developer.mozilla.org/en-US/docs/Web/Events/drag

This may also help FireFox onDrag Pagex PageY always zero ;(

  • I tried that, changing the pageX to screenX however that hasn't appeared to work. I did inspect the code thought, it appears each time the drag event is fired, it returns false (indicating that prevX doesn't change on drag). – Mr_incompetent Jan 30 '17 at 05:50
  • The onDrag zero seems to be the same issue, I put a console.log(prevX); statement at the first if statement, and it always returns zero. – Mr_incompetent Jan 30 '17 at 05:54
0

Unfortunately this is not supported by firefox Browser and W3C. Other browsers ignore this specification and use Mousemove instead, to address this specific issue. However you can use ondragstart and ondragend to get the co-ordinates of the specific event.

Santosh
  • 2,093
  • 1
  • 15
  • 21
  • Hi Santosh, yeah I found the same issue on a forum post from 2009, I'm surprised they haven't fixed it yet. I added an ondragover event listener at the document level to fix the issue, now it works perfectly. Thanks again for your help. – Mr_incompetent Jan 31 '17 at 01:16
  • Hi @Mr_incompetent, The bug is still open < https://bugzilla.mozilla.org/show_bug.cgi?id=505521 >. Hope it gets fixed soon. please mark this as answer if it helped. – Santosh Jan 31 '17 at 04:44