colandy Posted April 30, 2007 Share Posted April 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
Intelly XAD Posted April 30, 2007 Share Posted April 30, 2007 Hmm, what if you give adiv an ID and use document.getElementById('<your a id>').onclick = function() {} I think that should do the trick, hope it helps Grtz Quote Link to comment Share on other sites More sharing options...
colandy Posted April 30, 2007 Author Share Posted April 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.