problems with get cursor position

Discussion in 'ASP .Net' started by ton, Aug 21, 2008.

  1. ton

    ton Guest

    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
     
    ton, Aug 21, 2008
    #1
    1. Advertising

  2. "ton" <> wrote in message
    news:d05ce$48ad4866$541ef063$1.nb.home.nl...
    > 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.



    --
    Anthony Jones - MVP ASP/ASP.NET
     
    Anthony Jones, Aug 21, 2008
    #2
    1. Advertising

  3. ton

    ton Guest

    WOUW !!!!

    thnx anthony

    ton

    "Anthony Jones" <> schreef in bericht
    news:%...
    > "ton" <> wrote in message
    > news:d05ce$48ad4866$541ef063$1.nb.home.nl...
    >> 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.
    >
    >
    >
    > --
    > Anthony Jones - MVP ASP/ASP.NET
    >
    >
     
    ton, Aug 21, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaveR
    Replies:
    2
    Views:
    6,660
    Dave Rudlin
    Nov 17, 2004
  2. Peer W Hansen
    Replies:
    0
    Views:
    6,667
    Peer W Hansen
    May 16, 2004
  3. Alex9968
    Replies:
    0
    Views:
    601
    Alex9968
    Mar 19, 2008
  4. Alex9968
    Replies:
    0
    Views:
    1,628
    Alex9968
    Mar 19, 2008
  5. dmaziuk
    Replies:
    3
    Views:
    631
    Chris Gonnerman
    Jan 25, 2011
Loading...

Share This Page