Search code examples
javascriptpopupjsnipopuppanel

How to get the mouse position in JSNI


I have tried the following code but it is not working in full screen.

  public static native void hello1()
/*-{
   var body=$doc.getElementsByTagName("body")[0];
 var posx=0;
  var posy=0;

     body.addEventListener("click",function (e)
      {
    if (e.clientX ||e.clientY)
        {
            posx = e.clientX;
            posy = e.clientY;
        }
alert('Mouse position is: X='+posx+' Y='+posy);      
    });
 }-*/;

Solution

  •   public static native void hello1()
    /*-{
     var body=$doc.getElementsByTagName("body")[0];
     var posx;
     var posy;
    
       $wnd.addEventListener("click",function (e)
        {
         if (!e) var e = $wnd.event;
    if (e.pageX || e.pageY)
    {
        posx = e.pageX;
        posy = e.pageY;
    }
     else if (e.clientX ||e.clientY)
            {
                posx = e.clientX+$doc.body.scrolleft+$doc.scrollleft;
                posy = e.clientY+$doc.body.scrolltop+$doc.srolltop;
            }
    alert('Mouse position is: X='+posx+' Y='+posy);      
        });
      }-*/;