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

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

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

Post 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>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

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

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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.
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

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

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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.
Post Reply