Page 1 of 1

How do I detect scroll position? And how do I go to posit.?

Posted: Sun Jun 01, 2008 6:10 pm
by mirra1515
I have a script that inserts html tags around highlighted text; however, it has a glitch where when you insert a tag it automatically scrolls to the top of the textarea.

Therefore, I need to use scrollTop to get the location of the highlighted material, and then send the cursor back to that position once the button is pressed. I've tried looking for this on google, but all I find are auto scrolling scripts.

I'm guessing I would need to get the position onClick, and then after I have fun the function that adds the tag, have the JavaScript take the cursor to it's original position. Problem is, I don't know how to implement that.

Any help would be greatly appreciated :)

My code basically looks like this:

Code: Select all

<script type="JavaScript">
function addPTag(ta,pTag)
  {
     //Adds tags to the textarea at highlighted text location
  }
</script>
<body>
 
<button onclick="addPTag(document.getElementById('text'),'b')" value="bold"></button>
<textarea id="text" rows="20" cols="80">
Ipsum epsum lorem indige....
</textarea>
 
</body>

Re: How do I detect scroll position? And how do I go to posit.?

Posted: Sun Jun 01, 2008 7:48 pm
by califdon
I'm not sure this is what you want, but I have used this in a script to detect where the cursor is, with respect to fixed elements on the page:

Code: Select all

        if (navigator.appName == "Microsoft Internet Explorer") {
            scrlv = document.body.scrollTop;
        } else {
            scrlv = window.pageYOffset;
        }
As you can see, there is a scrollTop property of the document.body in IE, but Mozilla has, instead, pageYOffset property of the window object.

Re: How do I detect scroll position? And how do I go to posit.?

Posted: Sun Jun 01, 2008 8:12 pm
by mirra1515
califdon, thank you...I think thats what I need.

One thing though, once I have the location of the cursor stored to a var, how do I make the cursor go to that stored location if it is somewhere else in the textarea?

Re: How do I detect scroll position? And how do I go to posit.?

Posted: Sun Jun 01, 2008 8:49 pm
by Eran
Regarding the scrolling, I think it's better if you disabled the default behavior rather than scrolling back to where you were.
Events have the prevetDefault() method specifically for that purpose.

Re: How do I detect scroll position? And how do I go to posit.?

Posted: Mon Jun 02, 2008 1:24 am
by mirra1515
Thanks for your post, sounds like it could do the trick. I've looked up that command and have had little luck getting it to work for this purpose.

Any ideas on how I could implement it in my insertion script?

Re: How do I detect scroll position? And how do I go to posit.?

Posted: Mon Jun 02, 2008 4:13 am
by Eran
you need to return the event to the function handling the click event. The best way to do that is to use event listeners instead of direct using 'on' attributes. You can read a little bit about that here - http://www.quirksmode.org/js/events_advanced.html. You can still return the event object if you choose to use the onclick property - http://www.devguru.com/technologies/ecm ... event.html.

The event object has several methods and properties, one of which is preventDefault() which prevents the browser's default action from taking place.