problems with get cursor position

T

ton

Hi
I'm using this function:

function getPosition(e) {
e = e || window.event;
curs = {x:0, y:0};
if (e.pageX || e.pageY)
{curs.x = e.pageX;
curs.y = e.pageY; }
else
{curs.x = e.clientX +(document.documentElement.scrollLeft ||
document.body.scrollLeft) - document.documentElement.clientLeft;
curs.y = e.clientY +(document.documentElement.scrollTop ||
document.body.scrollTop) - document.documentElement.clientTop;}
}

when called in a function which was activited by:
"ondblclick" = "fAfspraak(" & ID & ");" the cursor values are calculted

but when called
.InnerHtml = "<a tabindex='1' href=fAfspraak(" & ID & ")' >" & text &
"</a>"

i receive a javascript error object requiered: there is no windows.event
(the function fAfspraak is called)

What is wrong here ?

thanx

ton
 
A

Anthony Jones

ton said:
Hi
I'm using this function:

function getPosition(e) {
e = e || window.event;
curs = {x:0, y:0};
if (e.pageX || e.pageY)
{curs.x = e.pageX;
curs.y = e.pageY; }
else
{curs.x = e.clientX +(document.documentElement.scrollLeft ||
document.body.scrollLeft) - document.documentElement.clientLeft;
curs.y = e.clientY +(document.documentElement.scrollTop ||
document.body.scrollTop) - document.documentElement.clientTop;}
}

when called in a function which was activited by:
"ondblclick" = "fAfspraak(" & ID & ");" the cursor values are calculted

but when called
.InnerHtml = "<a tabindex='1' href=fAfspraak(" & ID & ")' >" & text &
"</a>"

i receive a javascript error object requiered: there is no windows.event
(the function fAfspraak is called)

What is wrong here ?

There is no event in progress when that code runs. Try:-

"<a href=""javascript:void(0)"" onclick=""fAfspraak(" & ID & ")"""

BTW it looks like the code is expecting to be cross browser, how do you
intend to get a Mozilla event object into the getPosition function?

My preference is:-

<a href="javascript:void(0)" onclick="myFunc.apply(this, arguments)"
myID="12345">Click me</a>

Then in myFunc:-

function myFunc(e)
{
var id = this.getAttribute("myID")
var pos = getPosition(e)

// rest of your code

}

If you have dozens of these in a list consider:-

<div onclick="myFunc.apply(this, arguments)">
<a href="javascript:void(0)" myID="12345">Click me</a>
<a href="javascript:void(0)" myID="12346">No Click me</a>
</div>

The myFunc becomes:-

function myFunc(e)
{
var elem = e ? e.target : window.event.srcElement
var id = elem.getAttribute("myID")
var pos = getPosition(e)

// rest of your code
}

The generated html is smaller, the cost of event wire up (which can be
significant) is reduced, and your generating code looks cleaner especially
if you choose String.Format over the contentation you are currently doing.
 
T

ton

WOUW !!!!

thnx anthony

ton

Anthony Jones said:
There is no event in progress when that code runs. Try:-

"<a href=""javascript:void(0)"" onclick=""fAfspraak(" & ID & ")"""

BTW it looks like the code is expecting to be cross browser, how do you
intend to get a Mozilla event object into the getPosition function?

My preference is:-

<a href="javascript:void(0)" onclick="myFunc.apply(this, arguments)"
myID="12345">Click me</a>

Then in myFunc:-

function myFunc(e)
{
var id = this.getAttribute("myID")
var pos = getPosition(e)

// rest of your code

}

If you have dozens of these in a list consider:-

<div onclick="myFunc.apply(this, arguments)">
<a href="javascript:void(0)" myID="12345">Click me</a>
<a href="javascript:void(0)" myID="12346">No Click me</a>
</div>

The myFunc becomes:-

function myFunc(e)
{
var elem = e ? e.target : window.event.srcElement
var id = elem.getAttribute("myID")
var pos = getPosition(e)

// rest of your code
}

The generated html is smaller, the cost of event wire up (which can be
significant) is reduced, and your generating code looks cleaner especially
if you choose String.Format over the contentation you are currently doing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top