0

I'm helping to debug a website and sometimes i get this error : Object required on CWS.js line 45 character 3

// Track cursor position
var CWS_curPosX, CWS_curPosY;
document.onmousemove = CWS_MouseMove;

function CWS_MouseMove(evt)
{

if(window.Event)
{
    if(evt && evt.pageX)
    {
        CWS_curPosX = evt.pageX;
        CWS_curPosY = evt.pageY; 
    }
}
else
{
    CWS_curPosX = event.clientX + document.body.scrollLeft; // line 45
    CWS_curPosY = event.clientY + document.body.scrollTop;
}

I have no idea to solve this... Thanks in advance.

2 Answers 2

1

There is a couple of crucial isues with your code, primarily your use of window.Event - this if(...) condition will always evaluate false, as no browser supports this property on the window object. In actual fact the property has a lower case e in IE.

So this construct is often used, and looks similar to yours:

function someEventHandler(evt){
   if(window.event){ // Note lower case 'e' in 'event'
       // browser is IE, read properties of the event from window.event
   }
   else{
       // browser is probably some flavour of Mozilla, use the passed-in 'evt' 
       // parameter to read properties of the event
   }
}

Read this link (event object in different browsers) for more info.

This cross-browser difference is one of many reasons many people tend to use a framework such as jQuery which simplifies this event-handling code into one unified method of reading properties of the event.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your quick response, i try it now and i wait the tester's feedback.
A much simpler way is event = event || window.event
0

replace event to evt in 2 lines
CWS_curPosX = evt.clientX + document.body.scrollLeft;
CWS_curPosY = evt.clientY + document.body.scrollTop;

1 Comment

In case of the first comment doesn't work i will try your response, thank you for your quick response too!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.