lastIndexOf(caret)

I have a page at http://form.kr/dest/caret.php

The page need a parameter “caret”

if you click the link http://form.kr/dest/caret.php?caret=9, your cursor inside textarea will be blink between “9” and “0”.

if you click the link http://form.kr/dest/caret.php?caret=10, your cursor inside textarea will be blink between “0” and “a”.

if you click the link http://form.kr/dest/caret.php?caret=11, your cursor inside textarea will be blink between “a” and “b”.

Let’s suppose I cannot use the parameter “caret” but, instead, oldCaret.
But the parameter “oldCaret” doesn’t work at http://form.kr/dest/caret.php?oldCaret=10

So I made the code below at the page using PHP

if (isset($_GET['newCaret']) ) {
$location='?caret=' .$_GET['newCaret'];
header("location:$location");exit();

With the code above newCaret do work at http://form.kr/dest/caret.php?newCaret=10 by redirection to caret from newCaret.

I want the correct cursor position without redirection.

The trial code of PHP below doesn’t work.

$_GET['caret']=$_GET['oldCaret'];

Can I use newCaret without redirection by your help?

Hi @joon1, with your JS code

   var url=location.href;
   var no=url.substring(url.lastIndexOf("caret=")+6);
   setCaretPosition(document.getElementById("get"),no);

your not checking for the exact caret parameter anyway but just for any parameter that ends with caret. So you might simply convert the query string to lower case first in order to also match oldCaret and newCaret:

var params = window.location.search.toLowerCase()
var no = params.substring(url.lastIndexOf('caret=') + 6)
setCaretPosition(document.getElementById('get'), no)

I made another page with your code at http://form.kr/dest/caret2.php.

but
any of http://form.kr/dest/caret2.php?caret=10, http://form.kr/dest/caret2.php?newCaret=10, and http://form.kr/dest/caret2.php?oldCaret=10 seems not to work for cursor positioning.

Ah sry, yes I renamed the url variable to params but did not properly replace it everywhere… BTW you can see such errors in the console of the browser dev tools, which makes them easy enough to fix.

I removed “var no=url.substring(url.lastIndexOf(“caret=”)+6)”; at http://form.kr/dest/caret3.php?oldCaret=10

It seems not to work.

But why? You’d just have to replace the remaining url reference with params which I had forgot (or the other way round).

Of course not, now no is not defined… did you try checking the console?

At http://form.kr/dest/caret2.php, it says like the following

I don’t know how to fix it.

As I said, my mistake – just replace url with params (or the other way round).

Do you mean change url to param?
I change like the followng at http://form.kr/dest/caret5.php?caret=10

it seems not to work.

No to params.

I might have to give up this time.

Really? You just need to change param to params and you’re giving up before trying that one simple thing?

1 Like

http://form.kr/dest/caret6.php?oldcaret=10
I did.
Thank you, Paul_Wilkins and m3g4p0p

2 Likes

By the way, What is the literal meaning of caret?
Character set or something?

A caret is just an upside down V character, the ^ symbol that is sometimes used below a line of text to mark a position.

For example:

The third word is marked with a caret.
          ^