0

Possible Duplicate:
Javascript Error in FireFox Not in IE and Chrome

I have a JavaScript function which is in my Javacaleder.js file for Calender. This calendar works fine in IE and chrome but not working in Firefox. It shows error in Error console:

" Event is not defined "

It shows error in event.clientX; line. Below is the function; any help is appreciated. below function is called like this:

objDiv.style.left = GetControlLeftPosition_jScript() + "px";

function GetControlLeftPosition_jScript() {
    var controlLeft = event.clientX;
    var pageWidth = document.body.clientWidth;
    var spaceinRight = parseInt(pageWidth) - parseInt(controlLeft);
    if (spaceinRight > 220) {
        return controlLeft
    }
    else {
        return parseInt(pageWidth) - 220;
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mayank Pathak
  • 3,621
  • 5
  • 39
  • 67
  • 3
    Same reason like the question http://stackoverflow.com/questions/11256257/javascript-error-in-firefox-not-in-ie-and-chrome/11256291#11256291 – xdazz Jul 03 '12 at 06:50
  • A similar question has been answered here: http://stackoverflow.com/questions/2974601/event-is-not-defined-in-firefox-but-ok-in-chrome-and-ie – seedg Jul 03 '12 at 06:58
  • @Chris i don't have event object to pass as parameter in GetControlLeftPosition_jScript function.. as you can see the way it's being called...could you please explain if i pass event as parameter will this work if yes than why this should work ? – Mayank Pathak Jul 03 '12 at 07:02
  • those questions are different they have event object to pass..but i don't have access to event object it always show undefined..please help.. – Mayank Pathak Jul 03 '12 at 07:36
  • @MayankPathak, linked questions do explain why you are facing the problem. There is no global access to the event object in FF. Your next question should be 'how do I get access to that object?' ... For an answer, you will need to explain how is this code called. Are you setting the 'left' property in an event handler like click? – Amith George Jul 03 '12 at 08:50
  • You must be showing the calendar when someone clicks on the button or link or something, right? To capture that event, you have an event handler. That function will be passed the event object by FF. You check whether that object is null or not. if null, then access window.event, else use the passed in event object. – Amith George Jul 03 '12 at 08:53
  • @AmithGeorge i'm generating a div element in javascript using objdiv object and this function sets its left position..basically div is being converted to a calender..and i don't have access to event object in FF..what should i do now to get that access..can anyone throw some light on it.. ? – Mayank Pathak Jul 03 '12 at 08:55
  • Ok, from what I understand, the clientX property is used to get the horizontal position of the mouse wrt client area. Is that the value you want? Because the mouse could potentially be anywhere on the screen. Secondly, could you describe what action does the user do, for the calendar to be displayed? Does he simply load the page and the calendar pops up? Or he focuses on a text input field? – Amith George Jul 03 '12 at 09:11
  • User clicks on a button next to textbox and calender shows up..DateCotrol_jScript('Client id of textbox')... – Mayank Pathak Jul 03 '12 at 09:38

1 Answers1

1

User clicks on button next to textbox and then you show the calendar. Ok. Your click handler for the button will receive the event object as the first parameter. There, you have your event object!

Check if its undefined, if so, chances are you are running an older version of IE. in that case, use window.event

function buttonClickHandler(event) {
    var eventObj = event || window.event;
    objDiv.style.left = GetControlLeftPosition_jScript(eventObj) + "px";
}

function GetControlLeftPosition_jScript(eventObj) {
   var controlLeft = eventObj.clientX;
   //  ... remaining code.

That said, I would suggest you use a library that abstracts these concerns and instead provides you a single api. Something like jQuery or Mootools or dojo or whatever catches your fancy.

Amith George
  • 5,806
  • 2
  • 35
  • 53