开发者

How to calculate a 20 character string from a fixed session id and a non-fixed length page name

It's Friday afternoon and my brain is fried.

I have a unique server side ID stored in a cookie.

For site stat purposes i need to have a unique event identifier of no more than 20 characters which is made up of the page name and the remaining number of charcaters from the session id.

So if the page name = selectPlan that is 10 characters.

Therefore i would need the l开发者_StackOverflowast 10 characters from the session ID to complete the event identifier.

So far i have come up with:

var $JSESSIONID = $.cookies.get('JSESSIONID');
var $event22 = 'selectPlan';
var $remLength = 20 - $event22.length;
var $remSession = $JSESSIONID.substr(0,$remLength);
var $event22 = $event22 + $remSession;

alert($event22);

Am mostly confused about reading the JSESSIONID from the end rather then the start with substr.


isn't taking the last $remLength characters from $JSESSIONID just going to be

$JSESSIONID.substr( $JSESSIONID.length - $remLength, $remLength)?


if for example this characters were in a p tag and you wanted the last ten you would do:

var size = $('p').text().length;

var whatYouWant = $('p').text().slice(size - 10, size);

So i think you can take it from there.


You want the last $remLength characters from the session ID?

var $remSession = $JSESSIONID.substr( $JSESSIONID.length - $remLength, $remLength );

..assuming $JSESSIONID.length is always greater than 20.


One approach is to get the first N characters from the session id and replace them:

   var $JSESSIONID = $.cookies.get('JSESSIONID');
   var $event22 = 'selectPlan';

   //truncate sessionid to 20
   $JSESSIONID = $JSESSIONID.substr(0,20);
   //replace whatever's in the first 10 characters with your page name
   var result = $JSESSIONID.replace($JSESSIONID.substr(0, $event22.length), $event22);

   alert(result);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜