Jump to content

Javascript Firefox problem


colandy

Recommended Posts

I have the following code that creates a div and a link within that div, when the user clicks on the link the script should run a function which calculates the position of the mouse pointer.

 

In IE this works fine, however in Firefox I get an error relating to the event object. I have read that Firefox has an issue with the event object and using it this way.

 

var cdiv=document.createElement('div');

cdiv.setAttribute("id", "user");

var adiv=document.createElement('a');

adiv.setAttribute('href','#');

adiv.onclick=function()

{

var x=event.clientX;

var y=event.clientY;

alert("X - "+x+", Y - "+y);

}

adiv.appendChild(document.createTextNode(messages[(messages.length-1)-i]));

cdiv.appendChild(adiv);

 

Anyone got any idea's / Solutions...?!?!?!?!?

 

 

Please

 

Link to comment
Share on other sites

Nah.......found the solution though

 

adiv.onclick=function(e)

{

x=e.clientX;

y=e.clientY;

}

 

works in Firefox but not IE so have to check browser type then code accordingly. However I now have another issue. As the adiv is created within a loop (a list of members on a chat) using this method all links get the same link.

 

Heres the code:

 

for(var i=0;i<messages.length;i++)

{

  usrname=messages[(messages.length-1)-i];

  if(navigator.appName=="Microsoft Internet Explorer")

  {

    var cdiv=document.createElement('<div id="user">');

    var adiv=document.createElement('<a href="#" onclick="showpopup(event, \''+usrname+'\')">');

    adiv.appendChild(document.createTextNode(messages[(messages.length-1)-i]));

    cdiv.appendChild(adiv);

  }

  else

  {

    var cdiv=document.createElement('div');

    cdiv.setAttribute("id", "user");

    var adiv=document.createElement('a');

    adiv.setAttribute('href','#');

    adiv.onclick=function(e)

    {

      x=e.clientX;

      y=e.clientY;

      showpopup1(x,y,usrname);

    }

    adiv.appendChild(document.createTextNode(messages[(messages.length-1)-i]));

    cdiv.appendChild(adiv);

  }

  mdiv.appendChild(cdiv);

}

 

 

OK, when I have 1 member online both IE and Firefox report the X & Y position correctly and the username too. However, when 2 or more users online, X & Y positions are reported correctly on both and the username that was clicked on in IE is reported correctly, Firefox always reports the last listed user regardless of which you click on.

 

example firefox:

 

guest999 - reports guest000

guest000 - reports guest000

 

 

example IE

 

guest999 - reports guest999

guest000 - reports guest000

 

 

Any idea's...?!?!?!?

 

 

PS - The showpopup functions just display an alert box show X co-ords, Y co-ords and username passed to it.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.