remembering last position in textbox / textarea and returning to after post / reload

J

jason

I've got this javascript routine (i found on google - thank you) in an
asp.net page that on page reload sets the cursor of a textbox to the
last line. It works great!

Using a similar concept, I have another application that uses a
textbox like an editor window and has a save and other buttons.
Problem is - when I save/post/reload, the textbox returns to cursor
the top again.

How can I preserve / save the exact cursor spot and return to the
exact same spot I was in before I saved.

Here's the javascript code that sets the spot to the bottom of the
text. It called from <body onload=

<<<code>>>>

function f(){
var d=document;
if(d.getElementById){
var el=d.getElementById("chat");
if(el && el.createTextRange){
var rng=el.createTextRange(),
pos=el.value.length;
if(pos!=-1 && rng){
rng.moveStart("character",pos+1);
rng.collapse();
rng.select();

}
}
}
}
<<<code>>>>

I am imagining I will need to likely call the javascript with register
from asp.net. I'm just wondering how I can return position values to
serverside to save as a session variable - if that's even the best
way. Also, not clear on javascript code.

Thanks. Any help or information is greatly appreciated!
 
J

jason

Hello. More on this.

I have a javascript function that returns the location of the cursor
in the textbox. That javascript code is right in the asp.net code.
I've also added the asp.net code to create the javascript call to
that function. It's not working and when I do a view source it's
creating the function call inside my form???

this is the asp.net code I added at the end of sub that called from
button's onclick.

Dim sb As New System.Text.StringBuilder()
sb.Append("<script language='JavaScript'> getpos();")
sb.Append("</script"+chr(62))
Page.RegisterStartupScript("thescript",sb.ToString())

it's creating (from the view source):

code...
</textarea>
<input type="submit" name="edit" value="edit" id="edit" />
<script language='JavaScript'> getpos();</script>

</form>

Should this code be executing when I submit the form?

Thanks.
 
J

jason

closer... but stuck. (some of this code was collect from Google
sources thanks!)

The following two javascripts save and then restore current cursor
position in a textbox following form refreshes and submits. Problem is
it's far from exact as the cursor tends to slide (slightly) up or
down. I've tried adjusting the value of pos on putpos(), but can't get
it to leave the screen frozen in one spot following submits/saves like
a normal editor would. I'm thinking getpos() needs work. Or maybe all
of this is a waste of time as maybe there is some asp.net or
javascript function that preserve cursor position???

editor is the form
editwindow is the textbox
line is the field that sends/recieve cursor position between client
and server
pos is the cursor position
function putpos() is called in body onload=
function getpos() is called from a save(submit) button

Its proving to be a great example on how to marry client-side and
server-side

Thanks for any help!

<<code>>


function getpos() {
var ctrl=document.getElementById("editwindow");
var saveText = ctrl.value;
ctrl.focus();
var range = document.selection.createRange();
var weirdStr = String.fromCharCode(1);
range.text = weirdStr;
var pos = ctrl.value.indexOf(weirdStr);
ctrl.value = saveText;
range = ctrl.createTextRange();
range.move('character', pos);
range.select();
editor.line.value=pos;
editor.submit();

}


function putpos(){

pos=<%=Session("line")%>;
if (pos!=0){
var d=document;
if(d.getElementById){
var el=d.getElementById("editwindow");
if(el && el.createTextRange){
var rng=el.createTextRange();
if(pos!=-1 && rng){
rng.moveStart("character",pos);
rng.collapse();
rng.select();

}
}
}
}
}

</script>
 
J

Justin Lovell

No, it will not! If you want something like that, attach an client event
to the form so your code should look like this:

<form onsubmit="getpos();">
...
</form>

-- Justin Lovell
 

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,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top